Skip to content

add alias for lora adaptors #8636

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

Closed
wants to merge 1 commit into from
Closed

Conversation

zhipenghan
Copy link

@zhipenghan zhipenghan commented Jul 22, 2024

Added the ability to create aliases for adaptors, allowing for more intuitive and flexible naming conventions.
This feature simplifies referencing adaptors in different contexts, reducing potential errors and enhancing code clarity.

... --lora taskA_alias=ggml-lora-for-taskA-f16.gguf

@zhipenghan zhipenghan marked this pull request as ready for review July 22, 2024 23:32
@ngxson
Copy link
Collaborator

ngxson commented Jul 23, 2024

IMO it should not be the responsible of llama.cpp library to manage adapter name or alias.

The library already returns a pointer to the loaded adapter. User code can wrap that adapter into their own data struct, for example:

struct my_adapter {
  struct llama_lora_adapter * adapter;
  std::string alias;
};

...

struct my_adapter task_A{
  llama_lora_adapter_init(...),
  "task A",
};

Edit: also for wording, we prefer "adapter" over "adaptor", because it is referred in the original paper: https://arxiv.org/pdf/2106.09685

@@ -443,6 +443,10 @@ extern "C" {
struct llama_model * model,
struct llama_context_params params);

LLAMA_API void llama_ctx_switch_adaptor(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not the correct usage of lora adapter in llama.cpp

The adapter is loaded independently from inference context (llama_context). To control which adapter is enabled or not, use llama_lora_adapter_set and llama_lora_adapter_remove

See: #8332

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not the correct usage of lora adapter in llama.cpp

The adapter is loaded independently from inference context (llama_context). To control which adapter is enabled or not, use llama_lora_adapter_set and llama_lora_adapter_remove

See: #8332

Thanks for replying.
If the adaptors are expected to set/remove outside of the library. It should not be loaded while initialized the model.

IMO, the adaptors can be handled outsize the library, but the exist adaptors are unknown and invisiable. Need explicity way to clean current adaptors. It would be better to have llama_lora_adaptors_clean to clean exist adaptors.
In real scenarios, apply multiple adaptors on the foundation model is not common(let me know if there are cases). Apply one adaptor on foundation model is a common partten. And when setting a adaptor, usually we need clean exist adaptors outside of the library, but the exist adaptors are invisiable.

Copy link
Collaborator

@ngxson ngxson Jul 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the adaptors are expected to set/remove outside of the library. It should not be loaded while initialized the model.

I think there's small misconception here. llama.cpp comes with 2 main interfaces: llama_context and llama_model. When you load the model, tensors are stored in llama_model as read-only. If you want to use these tensors for inference, you need to pass the model to llama_context. This makes it possible to re-use one llama_model in multiple llama_context

The same idea applies to llama_lora_adapter. When adapter is loaded, you can reuse it in multiple different llama_context. Imagine one context can use adapter for task A and the other can use adapter for task B.

The list of active adapters is held by each llama_context (with the corresponding scales)

When you want to remove adapter from llama_context, use llama_lora_adapter_remove. To free an adapter (i.e. remove it completely from memory), use llama_lora_adapter_free. The adapters are also free if llama_model is freed, because adapter is linked to specific model.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the adaptors are expected to set/remove outside of the library. It should not be loaded while initialized the model.

I think there's small misconception here. llama.cpp comes with 2 main interfaces: llama_context and llama_model. When you load the model, tensors are stored in llama_model as read-only. If you want to use these tensors for inference, you need to pass the model to llama_context. This makes it possible to re-use one llama_model in multiple llama_context

The same idea applies to llama_lora_adapter. When adapter is loaded, you can reuse it in multiple different llama_context. Imagine one context can use adapter for task A and the other can use adapter for task B.

The list of active adapters is held by each llama_context (with the corresponding scales)

When you want to remove adapter from llama_context, use llama_lora_adapter_remove. To free an adapter (i.e. remove it completely from memory), use llama_lora_adapter_free. The adapters are also free if llama_model is freed, because adapter is linked to specific model.

After lctx, model loaded outside of the library:
std::tie(model, ctx) = llama_init_from_gpt_params(params);
The ctx handles the adaptors and invisible outside library scope. The inference context allocate the memory for computation graph calculation. The efficient way is reused the same context.
Or the adaptors have to be handled outside the library and prevent load any adaptors within the library scope.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO, the adaptors can be handled outsize the library, but the exist adaptors are unknown and invisiable. Need explicity way to clean current adaptors. It would be better to have llama_lora_adaptors_clean to clean exist adaptors.

I agree that common use case is one adapter per context, and llama_lora_adaptors_clean maybe useful to detach all adapters from a given context. But IMO llama.cpp should only provides most low-level API and user can build their own wrapper logic on top of it. Much like how operating systems only provide most basic syscalls, and libraries like glibc build on top of these syscalls.

In your case, user can simply write a llama_lora_adapter_switch(llama_lora_adapter * ptr) that automatically detach old adapter using llama_lora_adapter_remove, then attach the new one with llama_lora_adapter_set. It could be implement as a utility (see common folder).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO, the adaptors can be handled outsize the library, but the exist adaptors are unknown and invisiable. Need explicity way to clean current adaptors. It would be better to have llama_lora_adaptors_clean to clean exist adaptors.

I agree that common use case is one adapter per context, and llama_lora_adaptors_clean maybe useful to detach all adapters from a given context. But IMO llama.cpp should only provides most low-level API and user can build their own wrapper logic on top of it. Much like how operating systems only provide most basic syscalls, and libraries like glibc build on top of these syscalls.

In your case, user can simply write a llama_lora_adapter_switch(llama_lora_adapter * ptr) that automatically detach old adapter using llama_lora_adapter_remove, then attach the new one with llama_lora_adapter_set. It could be implement as a utility (see common folder).

Agree adding a llama_lora_adaptors_clear to clean exist adaptors. If this cleaning functionality is visible outside of the library’s scope, users can manipulate the adaptors outside of the library to customize them.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please have a look on #8653

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After lctx, model loaded outside of the library:
std::tie(model, ctx) = llama_init_from_gpt_params(params);
The ctx handles the adaptors and invisible outside library scope.

llama_init_from_gpt_params is not part of the library, it's only part of the examples. 3rd party applications should not rely on it.

@ngxson ngxson mentioned this pull request Jul 23, 2024
4 tasks
@ngxson
Copy link
Collaborator

ngxson commented Jul 24, 2024

I'll close this because the use case can be covered by #8653

@ngxson ngxson closed this Jul 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants