-
Notifications
You must be signed in to change notification settings - Fork 469
[Inference Providers] provider="auto"
#1390
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+163
−57
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
6ddcd18
auto select provider
hanouticelina bab28f9
nit
hanouticelina c7383d3
logging
hanouticelina 2d8e16e
Merge branch 'main' into auto-select-provider
hanouticelina 4a10479
fix
hanouticelina 591cdb6
Merge branch 'auto-select-provider' of github.com:huggingface/hugging…
hanouticelina 38530d0
rename InferenceProviderPolicy -> InferenceProviderOrPolicy
hanouticelina 83c809b
Merge branch 'main' into auto-select-provider
hanouticelina a4bce4f
remove unnecessary log
hanouticelina 38a4ab4
Merge branch 'auto-select-provider' of github.com:huggingface/hugging…
hanouticelina 29e8706
Merge branch 'main' into auto-select-provider
hanouticelina File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
---|---|---|
@@ -1,8 +1,8 @@ | ||
import type { WidgetType } from "@huggingface/tasks"; | ||
import type { InferenceProvider, ModelId } from "../types"; | ||
import { HF_HUB_URL } from "../config"; | ||
import { HARDCODED_MODEL_INFERENCE_MAPPING } from "../providers/consts"; | ||
import { EQUIVALENT_SENTENCE_TRANSFORMERS_TASKS } from "../providers/hf-inference"; | ||
import type { InferenceProvider, InferenceProviderOrPolicy, ModelId } from "../types"; | ||
import { typedInclude } from "../utils/typedInclude"; | ||
|
||
export const inferenceProviderMappingCache = new Map<ModelId, InferenceProviderMapping>(); | ||
|
@@ -20,44 +20,62 @@ export interface InferenceProviderModelMapping { | |
task: WidgetType; | ||
} | ||
|
||
export async function getInferenceProviderMapping( | ||
params: { | ||
accessToken?: string; | ||
modelId: ModelId; | ||
provider: InferenceProvider; | ||
task: WidgetType; | ||
}, | ||
options: { | ||
export async function fetchInferenceProviderMappingForModel( | ||
modelId: ModelId, | ||
accessToken?: string, | ||
options?: { | ||
fetch?: (input: RequestInfo, init?: RequestInit) => Promise<Response>; | ||
} | ||
): Promise<InferenceProviderModelMapping | null> { | ||
if (HARDCODED_MODEL_INFERENCE_MAPPING[params.provider][params.modelId]) { | ||
return HARDCODED_MODEL_INFERENCE_MAPPING[params.provider][params.modelId]; | ||
} | ||
): Promise<InferenceProviderMapping> { | ||
let inferenceProviderMapping: InferenceProviderMapping | null; | ||
if (inferenceProviderMappingCache.has(params.modelId)) { | ||
if (inferenceProviderMappingCache.has(modelId)) { | ||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
inferenceProviderMapping = inferenceProviderMappingCache.get(params.modelId)!; | ||
inferenceProviderMapping = inferenceProviderMappingCache.get(modelId)!; | ||
} else { | ||
const resp = await (options?.fetch ?? fetch)( | ||
`${HF_HUB_URL}/api/models/${params.modelId}?expand[]=inferenceProviderMapping`, | ||
`${HF_HUB_URL}/api/models/${modelId}?expand[]=inferenceProviderMapping`, | ||
{ | ||
headers: params.accessToken?.startsWith("hf_") ? { Authorization: `Bearer ${params.accessToken}` } : {}, | ||
headers: accessToken?.startsWith("hf_") ? { Authorization: `Bearer ${accessToken}` } : {}, | ||
} | ||
); | ||
if (resp.status === 404) { | ||
throw new Error(`Model ${params.modelId} does not exist`); | ||
throw new Error(`Model ${modelId} does not exist`); | ||
} | ||
inferenceProviderMapping = await resp | ||
.json() | ||
.then((json) => json.inferenceProviderMapping) | ||
.catch(() => null); | ||
|
||
if (inferenceProviderMapping) { | ||
inferenceProviderMappingCache.set(modelId, inferenceProviderMapping); | ||
} | ||
} | ||
|
||
if (!inferenceProviderMapping) { | ||
throw new Error(`We have not been able to find inference provider information for model ${params.modelId}.`); | ||
throw new Error(`We have not been able to find inference provider information for model ${modelId}.`); | ||
} | ||
return inferenceProviderMapping; | ||
} | ||
|
||
export async function getInferenceProviderMapping( | ||
params: { | ||
accessToken?: string; | ||
modelId: ModelId; | ||
provider: InferenceProvider; | ||
task: WidgetType; | ||
}, | ||
options: { | ||
fetch?: (input: RequestInfo, init?: RequestInit) => Promise<Response>; | ||
} | ||
): Promise<InferenceProviderModelMapping | null> { | ||
if (HARDCODED_MODEL_INFERENCE_MAPPING[params.provider][params.modelId]) { | ||
return HARDCODED_MODEL_INFERENCE_MAPPING[params.provider][params.modelId]; | ||
} | ||
const inferenceProviderMapping = await fetchInferenceProviderMappingForModel( | ||
params.modelId, | ||
params.accessToken, | ||
options | ||
); | ||
const providerMapping = inferenceProviderMapping[params.provider]; | ||
if (providerMapping) { | ||
const equivalentTasks = | ||
|
@@ -78,3 +96,23 @@ export async function getInferenceProviderMapping( | |
} | ||
return null; | ||
} | ||
|
||
export async function resolveProvider( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this could be done in |
||
provider?: InferenceProviderOrPolicy, | ||
modelId?: string | ||
): Promise<InferenceProvider> { | ||
if (!provider) { | ||
console.log( | ||
"Defaulting to 'auto' which will select the first provider available for the model, sorted by the user's order in https://hf.co/settings/inference-providers." | ||
); | ||
provider = "auto"; | ||
} | ||
if (provider === "auto") { | ||
if (!modelId) { | ||
throw new Error("Specifying a model is required when provider is 'auto'"); | ||
} | ||
const inferenceProviderMapping = await fetchInferenceProviderMappingForModel(modelId); | ||
provider = Object.keys(inferenceProviderMapping)[0] as InferenceProvider; | ||
} | ||
return provider; | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if
provider="auto"
, we callfetchInferenceProviderMappingForModel
2 times: once to resolve the provider and a second time inmakeRequestOptions
. this cache avoids the extra HTTP call.