Spaces:
Sleeping
Sleeping
Download code_copilot.livemd from osmanperviz/Code-Copilot: direct link, hf CLI and curl.
- Browser
- Download file 5.04 kB
-
https://huggingface.co/spaces/osmanperviz/Code-Copilot/resolve/main/code_copilot.livemd
- Command line
-
hf download hf://spaces/osmanperviz/Code-Copilot/code_copilot.livemd
-
curl -L -o code_copilot.livemd https://huggingface.co/spaces/osmanperviz/Code-Copilot/resolve/main/code_copilot.livemd
5.04 kB
| <!-- livebook:{"app_settings":{"slug":"osmanperviz-code-copilot"}} --> | |
| # Code Copilot | |
| ```elixir | |
| Mix.install( | |
| [ | |
| # Data Collection | |
| {:tentacat, "~> 2.0"}, | |
| # Machine Learning | |
| {:bumblebee, github: "elixir-nx/bumblebee", branch: "main"}, | |
| {:axon, github: "elixir-nx/axon", branch: "main", override: true}, | |
| {:polaris, "~> 0.1"}, | |
| {:exla, "~> 0.8", override: true}, | |
| {:nx, "~> 0.8", override: true} | |
| ], | |
| system_env: %{ | |
| "XLA_TARGET" => "cpu" | |
| } | |
| ) | |
| ``` | |
| ## Untitled | |
| ```elixir | |
| org = "elixir-nx" | |
| repos_dir = "repos" | |
| client = Tentacat.Client.new() | |
| {200, repos, _} = Tentacat.Repositories.list_orgs(client, org) | |
| ``` | |
| ## Section | |
| ```elixir | |
| File.mkdir_p!(repos_dir) | |
| Enum.each(repos, fn %{"full_name" => repo} -> | |
| [_, project] = String.split(repo, "/") | |
| path = Path.join([repos_dir, project]) | |
| if not File.exists?(path) do | |
| System.cmd("git", ["clone", "https://github.com/#{repo}", "#{repos_dir}/#{project}"]) | |
| end | |
| end) | |
| ``` | |
| ## Section | |
| ```elixir | |
| {:ok, tokenizer} = | |
| Bumblebee.load_tokenizer( | |
| {:hf, "deepseek-ai/deepseek-coder-1.3b-base", | |
| revision: "e94f2b11bc28abbd67ecadfaad058c30b24a589f"} | |
| ) | |
| tokenizer | |
| ``` | |
| ## Section | |
| ```elixir | |
| defmodule FIM do | |
| def permute(sample, opts \\ []) do | |
| opts = | |
| Keyword.validate!(opts, [ | |
| :prefix_token_id, | |
| :middle_token_id, | |
| :suffix_token_id, | |
| :pad_token_id, | |
| fim_rate: 0.5, | |
| fim_spm_rate: 0.5 | |
| ]) | |
| if :rand.uniform() > opts[:fim_rate] do | |
| [low, high] = Enum.take_random(1..(Nx.axis_size(sample, 0) - 1), 2) |> Enum.sort() | |
| prefix = sample[0..(low - 1)//1] | |
| middle = sample[low..(high - 1)//1] | |
| suffix = sample[high..-1//1] | |
| Nx.concatenate([ | |
| Nx.tensor([opts[:prefix_token_id], opts[:suffix_token_id]]), | |
| suffix, | |
| Nx.tensor([opts[:middle_token_id]]), | |
| prefix, | |
| middle | |
| ]) | |
| else | |
| pad_token = Nx.tensor([opts[:pad_token_id]]) | |
| Nx.concatenate([sample, pad_token, pad_token, pad_token]) | |
| end | |
| end | |
| end | |
| ``` | |
| ## Section | |
| ```elixir | |
| fim_prefix = "<|fim▁begin|>" | |
| fim_middle = "<|fim▁hole|>" | |
| fim_suffix = "<|fim▁end|>" | |
| pad_token = "<|end▁of▁sentence|>" | |
| prefix_token_id = Tokenizers.Tokenizer.token_to_id(tokenizer.native_tokenizer, fim_prefix) | |
| middle_token_id = Tokenizers.Tokenizer.token_to_id(tokenizer.native_tokenizer, fim_middle) | |
| suffix_token_id = Tokenizers.Tokenizer.token_to_id(tokenizer.native_tokenizer, fim_suffix) | |
| pad_token_id = Tokenizers.Tokenizer.token_to_id(tokenizer.native_tokenizer, pad_token) | |
| ``` | |
| ## Section | |
| ```elixir | |
| data = | |
| Path.wildcard(Path.join([repos_dir, "**", "*.{ex,exs}"])) | |
| |> Enum.map(fn file -> | |
| ["repos", project, path] = String.split(file, "/", parts: 3) | |
| contents = File.read!(file) | |
| %{project: project, path: path, contents: contents} | |
| end) | |
| ``` | |
| ## Section | |
| ```elixir | |
| max_seq_len = 128 | |
| batch_size = 8 | |
| train_data = | |
| data | |
| |> Stream.flat_map(fn %{contents: contents} -> | |
| tokenized = Bumblebee.apply_tokenizer(tokenizer, contents) | |
| tokenized["input_ids"] | |
| |> case do | |
| # this will discard some examples, but that's okay | |
| %{shape: {1, seq}} when seq < max_seq_len -> | |
| [] | |
| tensor -> | |
| tensor | |
| |> Nx.transpose() | |
| |> Nx.to_batched(max_seq_len - 3, leftover: :discard) | |
| |> Enum.map(&Nx.squeeze/1) | |
| end | |
| end) | |
| |> Stream.map( | |
| &FIM.permute(&1, | |
| prefix_token_id: prefix_token_id, | |
| middle_token_id: middle_token_id, | |
| suffix_token_id: suffix_token_id, | |
| pad_token_id: pad_token_id | |
| ) | |
| ) | |
| |> Stream.chunk_every(batch_size, batch_size, leftover: :discard) | |
| |> Stream.map(fn input_ids -> | |
| batch = Nx.stack(input_ids) | |
| {%{"input_ids" => batch}, batch} | |
| end) | |
| |> Stream.take(200) | |
| ``` | |
| ## Section | |
| ```elixir | |
| repo = {:hf, "deepseek-ai/deepseek-coder-1.3b-base"} | |
| {:ok, model_info} = Bumblebee.load_model(repo, backend: {EXLA.Backend, client: :host}) | |
| ``` | |
| ## Section | |
| ```elixir | |
| defmodule Trainer do | |
| import Nx.Defn | |
| defn causal_loss(labels, logits, opts \\ []) do | |
| opts = keyword!(opts, [:pad_token_id]) | |
| # shift logits left and labels right | |
| labels = labels[[.., 1..-1//1]] | |
| logits = logits[[.., 0..-2//1, ..]] | |
| padding_mask = Nx.equal(labels, opts[:pad_token_id]) | |
| Nx.select( | |
| padding_mask, | |
| 0.0, | |
| Axon.Losses.categorical_cross_entropy(Nx.new_axis(labels, -1), logits, | |
| from_logits: true, | |
| sparse: true | |
| ) | |
| ) | |
| |> Nx.mean() | |
| end | |
| end | |
| ``` | |
| ## Section | |
| ```elixir | |
| %{model: model, params: params} = model_info | |
| model = Axon.nx(model, & &1.logits) | |
| bf16 = {:bf, 16} | |
| mp_policy = Axon.MixedPrecision.create_policy(params: bf16, compute: bf16, output: bf16) | |
| mp_model = Axon.MixedPrecision.apply_policy(model, mp_policy) | |
| ``` | |
| ## Section | |
| ```elixir | |
| loss = &Trainer.causal_loss(&1, &2, pad_token_id: pad_token_id) | |
| optimizer = :sgd | |
| trained_model_state = | |
| mp_model | |
| |> Axon.Loop.trainer(loss, optimizer, loss_scale: :dynamic, log: 1) | |
| |> Axon.Loop.run(train_data, params, epochs: 1, compiler: EXLA) | |
| ``` | |
| ## Section | |