How we got fine-tuning Mistral-7B to not suck
helixml.substack.com
helixml.substack.com
Users tend to ask broad, vague questions of the document in order to test that the system is working. We want those queries to work well. For example, a user would ask "what are the doctors going to do?" of a document that is about a junior doctors' strike. Take this into account when generating the questions - in particular, refer to noun phrases by less specific descriptions, so for example instead of "junior doctors", say "doctors" in your questions.
[1]: https://github.com/helixml/helix/blob/main/api/pkg/dataprep/...
https://github.com/unslothai/unsloth
It’s my default now for experimenting and basic training. If I want to get into the weeds, I use axolotl, but 9/10, it’s not really necessary.
> Finetune Mistral, Llama 2-5x faster with 70% less memory!
Could be very useful for us!
Disclaimer: I work on Helix
People way understimate what RAG can do, even if in general people don't talk about the right things. For example LlamaIndex spends a lot of time talking about various extractors which is the easy part. The hard thing is deciding what you are actually searching for given a chat context.
RAG is a horrible hack (and the more you understand about the more it seems so!) but it does work.
I (and I'm sure everyone else) is experimenting with surgery on an LLM so it takes a vector representation of the docs directly alongside a text input so you don't have to do the lossy doc vector -> text -> LLM context -> vector thing. Not sure why no one has shipped this yet though!
I.e. One day we want to be able to backprop through the database.
Search systems face equivalent problems. The hierarchy of ML retrieval systems are separately optimized (trained). Maybe this helps regularize things, but, given enough compute / complexity, it is theoretically possible to differentiate through more of the stack.
Say you are trying to do RAG in a chat-type application. You do the following:
1) Summarize the context of chat into some text that is suitable for a search (lossy).
2) Turn this into a vector embedded in a particular vector space.
3) Use this vector to query a vector database, which returns reference to documents or document fragments (which themselves have been indexed as a lossy vector).
4) Take the text of these fragments and put them in the context of the LLM as input.
5) Modify the prompt to explain what these fragments are.
6) Then the prompt is sent to the LLM, which turns it into it's own vector representation.
An obvious improvement to this is that the VectorDB and the LLM should share an internal representation, and the VectorDB should understand this. The LLM should take this vector input as a second input alongside the text context and the LLM should combine them (in the same way you can put a text and image into a multi-modal model)
Isn't the vector representation of the text (and the ANN index itself) lossy, and the source text itself the source of truth?
I think many users get put off it because just pushing a button doesn’t work and the whole thing seems like a black box that you don’t know how to fix when it breaks.
It turns out that finetuning can be debugged, but the methods aren’t well documented (yet), eg by generating q/a, oversampling them, etc
When you get it to work it’s powerful - new abilities emerge beyond memorization.
Just like how llama2/claude2/gpt4 learned reasoning by memorizing sentences from Reddit posts :P
Also, I don’t get the comparison of rag vs finetuning in articles like this - why not do both. RAG is easy to setup - it’s push button. Just do it on all models (including finetuned models).
I'm interested to hear about push-button solutions for RAG that aren't a SaaS.
You can implement RAG in 80 lines of python and 0 SaaS libraries.
It's extremely easy.
1. Load your data as a giant string (streaming)
2. Chunk it (big chunk size, small chunk steps)
3. Call an LLM to convert chunk -> embedding, store in an index (or just concat it onto a numpy array)
4. Call an LLM to convert query -> embedding
5. Compute cosine similarity between the embeddings, pick the max
6. Insert the picked chunks into the LLM prompt
That's it. I'd encourage you to try to implement it yourself.
Anything beyond this is unnecessary complexity.
I walk through the code/whiteboard of the whole thing in this video: https://www.youtube.com/watch?v=Xkzd_YNbWmc&t=6003s
> Also, I don’t get the comparison of rag vs finetuning in articles like this - why not do both
It's interesting you say this because we are very close to adding RAG support to Helix sessions and it will be "both at the same time" not an "either or" setup. You can choose to do either or but we are interested in seeing if doing both at the same time yields better results than either or - watch this space!
disclaimer: I work on Helix
I often wonder how you'd go about organizing training data for a full historic github repo in a way that makes sense for training (or RAG)? The vast majority of the data is previous changes to the repo. I think this would generally mean that it would outweigh the current information and cause problems (i.e. old method names before refactoring etc.)
Also, perhaps being able to expand that out to doing the same thing for a bunch of consumers of the library that I'm maintaining would be neat.
Sprinkle in the PR and Issue history, docs website, API docs, and discord history and I think you'd have a helluva model.
> I often wonder how you'd go about organizing training data for a full historic github repo in a way that makes sense for training (or RAG)?
This is the hard part :-) But you are right - it would be intriguing to see what the output of a fune-tuned & RAG model would look like for this use-case. We are currently experimenting with adding RAG alongside the fine tuned model (so it's both, not either or) to see if it produces better results.
I will make sure we take a look at the gihub repo use case because it feels like that would be an interesting experiment to do!
disclaimer: I work on Helix
My only gripe with Helix would be that it's smaller than the above and my org would be peeved about data security. The ability to self host is cool, but too much can go wrong too quickly with plain Docker ML. Would love to see, for example, a `cog` version of the images that we can deploy distributed with more confidence/bravado.
[1] https://replicate.com/mistralai/mistral-7b-instruct-v0.2 [2] https://modal.com [3] https://llm-engine.scale.com/
It would be possible to include some parts of the new documents in the prompt so you can answer questions about new facts in the style and tone of your old documents, which we feel is useful. We are also experimenting with adding Retrieval Augmented Generation alongside fine tuning to see if the results are better than either or.
disclaimer: I work on Helix
Retrieval allows looking up facts - eg in a Google search
Finetuning allows reasoning using new knowledge.
Humans do both.
The most valuable skill an LLM can have is good reasoning skills and a broad enough knowledge base to understand. From there you can pass it the important bits it needs.
The key ingredients are:
reasoning(skills) + knowledge + important bits/facts
The best systems have all of these
We are also adding function calling so the model would know to reach out to an external API to fetch some data before generating a response.
disclaimer: I work on Helix
What we found was the IO latency for loading model weights into VRAM will kill responsiveness if you don't "re-use" sessions (i.e. where the model weights remain loaded and you run multiple inference sessions over the same loaded weights).
Obviously projects like https://github.com/vllm-project/vllm exist but we needed to build out a scheduler that can run a fleet of GPUs for a matrix of text/image vs inference/finetune sessions.
disclaimer: I work on Helix
The challenge arises when it becomes hard to generate that training data. If you just have the raw text and pop that in the context (i.e. RAG), then the LLM can be just as factual without any of that hassle.
Q2: identifiers in the prompt to say "you've been trained on this, only answer questions about this".
Q3: Depends on the size of the training data/docs. For the average PDF, about 30 minutes.
Give it a try!
pigeon-hole?
In practice we've found that it's a bit of a balancing act to teach the model the new knowledge without destroying existing knowledge, but it's just a matter of tuning the parameters carefully. We're also researching whether we can fine-tune a brand new expert in a MoE model like Mixtral, I've also seen work on fine-tuning just a fixed set of weights. I'm sure there will be more developments in this space soon.
In terms of how you refer to new knowledge and not base knowledge, like many things in LLMs, you just ask the LLM :-) For example, if you look at this session https://app.tryhelix.ai/session/62905598-b1b7-4d93-bc39-5a93... and click "Show Info" at the top, you can see the system prompt is:
"You are an intelligent chatbot named Helix that has been fine-tuned on document(s) e1ef2e896c in document group 62905598b1. The document group contains 1 document(s). The user will ask you questions about these documents: you must ONLY answer with context from the documents listed. Do NOT refer to background knowledge."
It does a pretty good job at this, although I'm sure there are ways to improve it further.
Referencing the specific document IDs in the fine-tuning was an innovation that has really helped us.
In terms of training time, yeah - 5 minutes on a news article, 10 minutes on a typical length paper. Pretty usable. We're experimenting with reducing the number of epochs and increasing the learning rate to make it faster at that too.