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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<!-- livebook:{"app_settings":{"slug":"osmanperviz-code-copilot"}} -->
|
| 2 |
+
|
| 3 |
+
# Code Copilot
|
| 4 |
+
|
| 5 |
+
```elixir
|
| 6 |
+
Mix.install(
|
| 7 |
+
[
|
| 8 |
+
# Data Collection
|
| 9 |
+
{:tentacat, "~> 2.0"},
|
| 10 |
+
# Machine Learning
|
| 11 |
+
{:bumblebee, github: "elixir-nx/bumblebee", branch: "main"},
|
| 12 |
+
{:axon, github: "elixir-nx/axon", branch: "main", override: true},
|
| 13 |
+
{:polaris, "~> 0.1"},
|
| 14 |
+
{:exla, "~> 0.8", override: true},
|
| 15 |
+
{:nx, "~> 0.8", override: true}
|
| 16 |
+
],
|
| 17 |
+
system_env: %{
|
| 18 |
+
"XLA_TARGET" => "cpu"
|
| 19 |
+
}
|
| 20 |
+
)
|
| 21 |
+
```
|
| 22 |
+
|
| 23 |
+
## Untitled
|
| 24 |
+
|
| 25 |
+
```elixir
|
| 26 |
+
org = "elixir-nx"
|
| 27 |
+
repos_dir = "repos"
|
| 28 |
+
|
| 29 |
+
client = Tentacat.Client.new()
|
| 30 |
+
{200, repos, _} = Tentacat.Repositories.list_orgs(client, org)
|
| 31 |
+
```
|
| 32 |
+
|
| 33 |
+
## Section
|
| 34 |
+
|
| 35 |
+
```elixir
|
| 36 |
+
File.mkdir_p!(repos_dir)
|
| 37 |
+
|
| 38 |
+
Enum.each(repos, fn %{"full_name" => repo} ->
|
| 39 |
+
[_, project] = String.split(repo, "/")
|
| 40 |
+
path = Path.join([repos_dir, project])
|
| 41 |
+
|
| 42 |
+
if not File.exists?(path) do
|
| 43 |
+
System.cmd("git", ["clone", "https://github.com/#{repo}", "#{repos_dir}/#{project}"])
|
| 44 |
+
end
|
| 45 |
+
end)
|
| 46 |
+
```
|
| 47 |
+
|
| 48 |
+
## Section
|
| 49 |
+
|
| 50 |
+
```elixir
|
| 51 |
+
{:ok, tokenizer} =
|
| 52 |
+
Bumblebee.load_tokenizer(
|
| 53 |
+
{:hf, "deepseek-ai/deepseek-coder-1.3b-base",
|
| 54 |
+
revision: "e94f2b11bc28abbd67ecadfaad058c30b24a589f"}
|
| 55 |
+
)
|
| 56 |
+
|
| 57 |
+
tokenizer
|
| 58 |
+
```
|
| 59 |
+
|
| 60 |
+
## Section
|
| 61 |
+
|
| 62 |
+
```elixir
|
| 63 |
+
defmodule FIM do
|
| 64 |
+
def permute(sample, opts \\ []) do
|
| 65 |
+
opts =
|
| 66 |
+
Keyword.validate!(opts, [
|
| 67 |
+
:prefix_token_id,
|
| 68 |
+
:middle_token_id,
|
| 69 |
+
:suffix_token_id,
|
| 70 |
+
:pad_token_id,
|
| 71 |
+
fim_rate: 0.5,
|
| 72 |
+
fim_spm_rate: 0.5
|
| 73 |
+
])
|
| 74 |
+
|
| 75 |
+
if :rand.uniform() > opts[:fim_rate] do
|
| 76 |
+
[low, high] = Enum.take_random(1..(Nx.axis_size(sample, 0) - 1), 2) |> Enum.sort()
|
| 77 |
+
|
| 78 |
+
prefix = sample[0..(low - 1)//1]
|
| 79 |
+
middle = sample[low..(high - 1)//1]
|
| 80 |
+
suffix = sample[high..-1//1]
|
| 81 |
+
|
| 82 |
+
Nx.concatenate([
|
| 83 |
+
Nx.tensor([opts[:prefix_token_id], opts[:suffix_token_id]]),
|
| 84 |
+
suffix,
|
| 85 |
+
Nx.tensor([opts[:middle_token_id]]),
|
| 86 |
+
prefix,
|
| 87 |
+
middle
|
| 88 |
+
])
|
| 89 |
+
else
|
| 90 |
+
pad_token = Nx.tensor([opts[:pad_token_id]])
|
| 91 |
+
Nx.concatenate([sample, pad_token, pad_token, pad_token])
|
| 92 |
+
end
|
| 93 |
+
end
|
| 94 |
+
end
|
| 95 |
+
```
|
| 96 |
+
|
| 97 |
+
## Section
|
| 98 |
+
|
| 99 |
+
```elixir
|
| 100 |
+
fim_prefix = "<|fim▁begin|>"
|
| 101 |
+
fim_middle = "<|fim▁hole|>"
|
| 102 |
+
fim_suffix = "<|fim▁end|>"
|
| 103 |
+
pad_token = "<|end▁of▁sentence|>"
|
| 104 |
+
|
| 105 |
+
prefix_token_id = Tokenizers.Tokenizer.token_to_id(tokenizer.native_tokenizer, fim_prefix)
|
| 106 |
+
middle_token_id = Tokenizers.Tokenizer.token_to_id(tokenizer.native_tokenizer, fim_middle)
|
| 107 |
+
suffix_token_id = Tokenizers.Tokenizer.token_to_id(tokenizer.native_tokenizer, fim_suffix)
|
| 108 |
+
pad_token_id = Tokenizers.Tokenizer.token_to_id(tokenizer.native_tokenizer, pad_token)
|
| 109 |
+
```
|
| 110 |
+
|
| 111 |
+
## Section
|
| 112 |
+
|
| 113 |
+
```elixir
|
| 114 |
+
data =
|
| 115 |
+
Path.wildcard(Path.join([repos_dir, "**", "*.{ex,exs}"]))
|
| 116 |
+
|> Enum.map(fn file ->
|
| 117 |
+
["repos", project, path] = String.split(file, "/", parts: 3)
|
| 118 |
+
contents = File.read!(file)
|
| 119 |
+
%{project: project, path: path, contents: contents}
|
| 120 |
+
end)
|
| 121 |
+
```
|
| 122 |
+
|
| 123 |
+
## Section
|
| 124 |
+
|
| 125 |
+
```elixir
|
| 126 |
+
max_seq_len = 128
|
| 127 |
+
batch_size = 8
|
| 128 |
+
|
| 129 |
+
train_data =
|
| 130 |
+
data
|
| 131 |
+
|> Stream.flat_map(fn %{contents: contents} ->
|
| 132 |
+
tokenized = Bumblebee.apply_tokenizer(tokenizer, contents)
|
| 133 |
+
|
| 134 |
+
tokenized["input_ids"]
|
| 135 |
+
|> case do
|
| 136 |
+
# this will discard some examples, but that's okay
|
| 137 |
+
%{shape: {1, seq}} when seq < max_seq_len ->
|
| 138 |
+
[]
|
| 139 |
+
|
| 140 |
+
tensor ->
|
| 141 |
+
tensor
|
| 142 |
+
|> Nx.transpose()
|
| 143 |
+
|> Nx.to_batched(max_seq_len - 3, leftover: :discard)
|
| 144 |
+
|> Enum.map(&Nx.squeeze/1)
|
| 145 |
+
end
|
| 146 |
+
end)
|
| 147 |
+
|> Stream.map(
|
| 148 |
+
&FIM.permute(&1,
|
| 149 |
+
prefix_token_id: prefix_token_id,
|
| 150 |
+
middle_token_id: middle_token_id,
|
| 151 |
+
suffix_token_id: suffix_token_id,
|
| 152 |
+
pad_token_id: pad_token_id
|
| 153 |
+
)
|
| 154 |
+
)
|
| 155 |
+
|> Stream.chunk_every(batch_size, batch_size, leftover: :discard)
|
| 156 |
+
|> Stream.map(fn input_ids ->
|
| 157 |
+
batch = Nx.stack(input_ids)
|
| 158 |
+
{%{"input_ids" => batch}, batch}
|
| 159 |
+
end)
|
| 160 |
+
|> Stream.take(200)
|
| 161 |
+
```
|
| 162 |
+
|
| 163 |
+
## Section
|
| 164 |
+
|
| 165 |
+
```elixir
|
| 166 |
+
repo = {:hf, "deepseek-ai/deepseek-coder-1.3b-base"}
|
| 167 |
+
{:ok, model_info} = Bumblebee.load_model(repo, backend: {EXLA.Backend, client: :host})
|
| 168 |
+
```
|
| 169 |
+
|
| 170 |
+
## Section
|
| 171 |
+
|
| 172 |
+
```elixir
|
| 173 |
+
defmodule Trainer do
|
| 174 |
+
import Nx.Defn
|
| 175 |
+
|
| 176 |
+
defn causal_loss(labels, logits, opts \\ []) do
|
| 177 |
+
opts = keyword!(opts, [:pad_token_id])
|
| 178 |
+
# shift logits left and labels right
|
| 179 |
+
labels = labels[[.., 1..-1//1]]
|
| 180 |
+
logits = logits[[.., 0..-2//1, ..]]
|
| 181 |
+
|
| 182 |
+
padding_mask = Nx.equal(labels, opts[:pad_token_id])
|
| 183 |
+
|
| 184 |
+
Nx.select(
|
| 185 |
+
padding_mask,
|
| 186 |
+
0.0,
|
| 187 |
+
Axon.Losses.categorical_cross_entropy(Nx.new_axis(labels, -1), logits,
|
| 188 |
+
from_logits: true,
|
| 189 |
+
sparse: true
|
| 190 |
+
)
|
| 191 |
+
)
|
| 192 |
+
|> Nx.mean()
|
| 193 |
+
end
|
| 194 |
+
end
|
| 195 |
+
```
|
| 196 |
+
|
| 197 |
+
## Section
|
| 198 |
+
|
| 199 |
+
```elixir
|
| 200 |
+
%{model: model, params: params} = model_info
|
| 201 |
+
|
| 202 |
+
model = Axon.nx(model, & &1.logits)
|
| 203 |
+
|
| 204 |
+
bf16 = {:bf, 16}
|
| 205 |
+
mp_policy = Axon.MixedPrecision.create_policy(params: bf16, compute: bf16, output: bf16)
|
| 206 |
+
mp_model = Axon.MixedPrecision.apply_policy(model, mp_policy)
|
| 207 |
+
```
|
| 208 |
+
|
| 209 |
+
## Section
|
| 210 |
+
|
| 211 |
+
```elixir
|
| 212 |
+
loss = &Trainer.causal_loss(&1, &2, pad_token_id: pad_token_id)
|
| 213 |
+
|
| 214 |
+
optimizer = :sgd
|
| 215 |
+
|
| 216 |
+
trained_model_state =
|
| 217 |
+
mp_model
|
| 218 |
+
|> Axon.Loop.trainer(loss, optimizer, loss_scale: :dynamic, log: 1)
|
| 219 |
+
|> Axon.Loop.run(train_data, params, epochs: 1, compiler: EXLA)
|
| 220 |
+
```
|
| 221 |
+
|
| 222 |
+
## Section
|