-
Notifications
You must be signed in to change notification settings - Fork 905
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[perf] Reduce eval local to remote tracking latency by caching the ar…
…m token (#3427) # Description Based on the investigation, local to remote tracking involves multiple client to service calls. Each call requires acquiring an ARM token from AAD, with each token acquisition taking about 2 seconds. By caching the token, we could reduce the end-to-end time of the evaluate API call with one evaluator from 76 seconds to 51 seconds, achieving around a 30% improvement. For more details, please check out [here](https://microsoft-my.sharepoint.com/:w:/p/ninhu/ETB_zdMkFrdAuf3Lcg9ssrUB6RVmyuFs5Un1G74O1HlwSA?e=cBVmsw) # All Promptflow Contribution checklist: - [ ] **The pull request does not introduce [breaking changes].** - [ ] **CHANGELOG is updated for new features, bug fixes or other significant changes.** - [ ] **I have read the [contribution guidelines](../CONTRIBUTING.md).** - [ ] **Create an issue and link to the pull request to get dedicated review from promptflow team. Learn more: [suggested workflow](../CONTRIBUTING.md#suggested-workflow).** ## General Guidelines and Best Practices - [ ] Title of the pull request is clear and informative. - [ ] There are a small number of commits, each of which have an informative message. This means that previously merged commits do not appear in the history of the PR. For more information on cleaning up the commits in your PR, [see this page](https://github.com/Azure/azure-powershell/blob/master/documentation/development-docs/cleaning-up-commits.md). ### Testing Guidelines - [ ] Pull request includes test coverage for the included changes.
- Loading branch information
Showing
5 changed files
with
249 additions
and
181 deletions.
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
src/promptflow-azure/promptflow/azure/_utils/_token_cache.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# --------------------------------------------------------- | ||
# Copyright (c) Microsoft Corporation. All rights reserved. | ||
# --------------------------------------------------------- | ||
import time | ||
|
||
import jwt | ||
|
||
from promptflow.core._connection_provider._utils import get_arm_token | ||
|
||
|
||
class SingletonMeta(type): | ||
_instances = {} | ||
|
||
def __call__(cls, *args, **kwargs): | ||
if cls not in cls._instances: | ||
instance = super().__call__(*args, **kwargs) | ||
cls._instances[cls] = instance | ||
return cls._instances[cls] | ||
|
||
|
||
class ArmTokenCache(metaclass=SingletonMeta): | ||
TOKEN_REFRESH_THRESHOLD_SECS = 300 | ||
|
||
def __init__(self): | ||
self._cache = {} | ||
|
||
def _is_token_valid(self, entry): | ||
current_time = time.time() | ||
return (entry["expires_at"] - current_time) >= self.TOKEN_REFRESH_THRESHOLD_SECS | ||
|
||
def get_token(self, credential): | ||
if credential in self._cache: | ||
entry = self._cache[credential] | ||
if self._is_token_valid(entry): | ||
return entry["token"] | ||
|
||
token = self._fetch_token(credential) | ||
decoded_token = jwt.decode(token, options={"verify_signature": False, "verify_aud": False}) | ||
expiration_time = decoded_token.get("exp", time.time()) | ||
self._cache[credential] = {"token": token, "expires_at": expiration_time} | ||
return token | ||
|
||
def _fetch_token(self, credential): | ||
return get_arm_token(credential=credential) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.