From 6b9fa93edf21f2380a315be3b65f13ca698752c1 Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Sat, 30 Nov 2024 21:50:23 -0800 Subject: [PATCH] [mojo-lang] Rename `inout` -> `mut` and `borrowed` -> `read` Per extensive discussion over on this public thread: https://github.com/modularml/mojo/issues/3623 We're moving to rename the `inout` argument convention to be called simply `mut`, and renames `borrowed` to `read` which can still be generally elided. This reduces the need to understand references for the basic conventions that many people work with, while providing a more strictly-correct and consistent model. These words are now "soft" keywords instead of "hard" keywords as well. This still maintains support for the `inout` and `borrowed` keywords, though they will eventually be removed. MODULAR_ORIG_COMMIT_REV_ID: e2b41cfb4cb8bb0b2e67ade93d32d7ef8989428e --- .../benchmarks/human_eval.\360\237\224\245" | 2 +- .../pipelines/configs/llama.\360\237\224\245" | 2 +- .../pipelines/llama2/model.\360\237\224\245" | 4 ++-- .../pipelines/llama2/run.\360\237\224\245" | 14 ++++++------ .../arena_linked_list.\360\237\224\245" | 4 ++-- .../llama2/tokenizer/bpe.\360\237\224\245" | 6 ++--- .../tokenizer/max_heap.\360\237\224\245" | 8 +++---- .../llama3/kv_cache.\360\237\224\245" | 4 ++-- .../llama3/model/llama.\360\237\224\245" | 4 ++-- .../pipelines/llama3/run.\360\237\224\245" | 10 ++++----- .../metrics/metrics.\360\237\224\245" | 22 +++++++++---------- .../nn/transformer.\360\237\224\245" | 2 +- .../load_tinystories.\360\237\224\245" | 2 +- .../quantize_tinystories.\360\237\224\245" | 4 ++-- .../pipelines/replit/bpe_tokenizer/ball.mojo | 4 ++-- .../pipelines/replit/bpe_tokenizer/json.mojo | 4 ++-- .../replit/bpe_tokenizer/max_heap.mojo | 10 ++++----- .../graph-api/pipelines/replit/config.mojo | 2 +- .../pipelines/replit/model/replit.mojo | 2 +- examples/graph-api/pipelines/replit/run.mojo | 18 +++++++-------- .../replit/tokenizer/autotokenizer.mojo | 2 +- .../pipelines/replit/tokenizer/tokenizer.mojo | 2 +- .../arena_linked_list.\360\237\224\245" | 4 ++-- .../tokenizer/autotokenizer.\360\237\224\245" | 2 +- .../pipelines/tokenizer/bpe.\360\237\224\245" | 6 ++--- .../tokenizer/max_heap.\360\237\224\245" | 8 +++---- .../graph-api/pipelines/tokenizer/regex.mojo | 12 +++++----- .../pipelines/tokenizer/tiktoken.mojo | 6 ++--- .../tokenizer/tokenizer.\360\237\224\245" | 2 +- .../weights/ggml_quants.\360\237\224\245" | 8 +++---- .../pipelines/weights/gguf.\360\237\224\245" | 20 ++++++++--------- .../llama2checkpoint.\360\237\224\245" | 6 ++--- .../weights/loadable_model.\360\237\224\245" | 8 +++---- .../test/configs/test_parse_args.mojo | 2 +- .../graph-api/test/llama3/test_layers.mojo | 2 +- .../graph-api/test/replit/test_logits.mojo | 4 ++-- .../scheduler.\360\237\224\245" | 4 ++-- 37 files changed, 113 insertions(+), 113 deletions(-) diff --git "a/examples/graph-api/pipelines/benchmarks/human_eval.\360\237\224\245" "b/examples/graph-api/pipelines/benchmarks/human_eval.\360\237\224\245" index 78bd98ce..639d96b0 100644 --- "a/examples/graph-api/pipelines/benchmarks/human_eval.\360\237\224\245" +++ "b/examples/graph-api/pipelines/benchmarks/human_eval.\360\237\224\245" @@ -62,7 +62,7 @@ struct HumanEval: def get_problems(self) -> PythonObject: return self._human_eval_module.read_problems() - def add_sample(inout self, task_id: PythonObject, completion: String): + def add_sample(mut self, task_id: PythonObject, completion: String): sample = Python.dict() sample["task_id"] = task_id sample["completion"] = PythonObject(completion) diff --git "a/examples/graph-api/pipelines/configs/llama.\360\237\224\245" "b/examples/graph-api/pipelines/configs/llama.\360\237\224\245" index 91aed0fb..71c157af 100644 --- "a/examples/graph-api/pipelines/configs/llama.\360\237\224\245" +++ "b/examples/graph-api/pipelines/configs/llama.\360\237\224\245" @@ -55,7 +55,7 @@ struct LlamaConfigRegistry(ConfigRegistry): var registry: ConfigRegistryDict def __init__( - inout self, + mut self, additional_pipeline_args: ConfigRegistryDict = ConfigRegistryDict(), ): """ diff --git "a/examples/graph-api/pipelines/llama2/model.\360\237\224\245" "b/examples/graph-api/pipelines/llama2/model.\360\237\224\245" index dc49eb03..ca1f3e85 100644 --- "a/examples/graph-api/pipelines/llama2/model.\360\237\224\245" +++ "b/examples/graph-api/pipelines/llama2/model.\360\237\224\245" @@ -72,7 +72,7 @@ struct QuantizedLlama2[ fn __init__(out self, owned model: model_type): self.model = model^ - def build_graph(inout self, name: String) -> Graph: + def build_graph(mut self, name: String) -> Graph: params = self.hyperparams() cache_type = TensorType( DType.float32, @@ -188,7 +188,7 @@ struct Llama2[ def __init__(out self, owned model: model_type): self.model = model^ - def build_graph(inout self, name: String) -> Graph: + def build_graph(mut self, name: String) -> Graph: params = self.model.hyperparams() cache_type = OpaqueType( diff --git "a/examples/graph-api/pipelines/llama2/run.\360\237\224\245" "b/examples/graph-api/pipelines/llama2/run.\360\237\224\245" index 7d6bfbd1..54682f32 100644 --- "a/examples/graph-api/pipelines/llama2/run.\360\237\224\245" +++ "b/examples/graph-api/pipelines/llama2/run.\360\237\224\245" @@ -97,7 +97,7 @@ struct Config: "Alternatively provide a `--quantization-encoding`" ) - fn get(inout self, key: String) raises -> OptionValue: + fn get(mut self, key: String) raises -> OptionValue: """Returns an option value for `key` in the underlying config. Args: @@ -111,7 +111,7 @@ struct Config: """ return self.config[key] - fn set(inout self, key: String, val: OptionValue): + fn set(mut self, key: String, val: OptionValue): """Sets a new value for a given config key. This will overwrite the old value if the key is already present. @@ -146,11 +146,11 @@ def _get_attention_mask( def _generate_q_text_with_tokenizer[ tokenizer_type: Tokenizer, ]( - inout tokenizer: tokenizer_type, + mut tokenizer: tokenizer_type, compiled_model: Model, params: LlamaHParams, config: Config, - inout metrics: Metrics, + mut metrics: Metrics, execution_device: Device, ): host_device = cpu_device() @@ -228,11 +228,11 @@ def _generate_text_with_tokenizer[ tokenizer_type: Tokenizer, kv_params: KVCacheStaticParams, ]( - inout tokenizer: tokenizer_type, + mut tokenizer: tokenizer_type, compiled_model: Model, params: LlamaHParams, config: Config, - inout metrics: Metrics, + mut metrics: Metrics, execution_device: Device, ): host_device = cpu_device() @@ -351,7 +351,7 @@ def generate_text( params: LlamaHParams, config: Config, execution_device: Device, - inout metrics: Metrics, + mut metrics: Metrics, ): """Generated text by applying the compiled model to the provided prompt.""" mojo_tokenizer = BPETokenizer.from_file(config.get("tokenizer-path")[Path]) diff --git "a/examples/graph-api/pipelines/llama2/tokenizer/arena_linked_list.\360\237\224\245" "b/examples/graph-api/pipelines/llama2/tokenizer/arena_linked_list.\360\237\224\245" index a9e3c3f5..ca1495df 100644 --- "a/examples/graph-api/pipelines/llama2/tokenizer/arena_linked_list.\360\237\224\245" +++ "b/examples/graph-api/pipelines/llama2/tokenizer/arena_linked_list.\360\237\224\245" @@ -62,7 +62,7 @@ struct ArenaLinkedList[ElementType: CollectionElement]: """Checks whether the node is still in the list.""" return 0 <= id < len(self._arena) and self.node(id) - def append(inout self, owned value: ElementType) -> Self.ID: + def append(mut self, owned value: ElementType) -> Self.ID: """Adds a new element to the back of the list.""" id = len(self._arena) node = Node[ElementType](value^, self._tail, None) @@ -74,7 +74,7 @@ struct ArenaLinkedList[ElementType: CollectionElement]: self._arena.append(node) return id - def remove(inout self, id: Self.ID): + def remove(mut self, id: Self.ID): """Removes an element from the list.""" debug_assert(bool(self.node(id)), "removing item not in list") debug_assert(bool(self._head), "removing from empty list") diff --git "a/examples/graph-api/pipelines/llama2/tokenizer/bpe.\360\237\224\245" "b/examples/graph-api/pipelines/llama2/tokenizer/bpe.\360\237\224\245" index 1677eb65..ced6e16f 100644 --- "a/examples/graph-api/pipelines/llama2/tokenizer/bpe.\360\237\224\245" +++ "b/examples/graph-api/pipelines/llama2/tokenizer/bpe.\360\237\224\245" @@ -45,7 +45,7 @@ struct MergeOption(OrderedElement): ) -def read[T: CollectionElement](inout span: Span[Byte, _]) -> T: +def read[T: CollectionElement](mut span: Span[Byte, _]) -> T: """Read a binary type out of a byte buffer and increment the pointer.""" value = span.unsafe_ptr().bitcast[T]()[] span = span[sizeof[T]() :] @@ -110,13 +110,13 @@ struct BPETokenizer(Tokenizer): with open(path, "r") as file: return Self.from_bytes(file.read()) - def add_token(inout self, token: String, score: Float32): + def add_token(mut self, token: String, score: Float32): """Add a token to the vocabulary.""" if token not in self.token_ids: self.token_ids[token] = len(self.vocab) self.vocab.append(Token(token, score)) - def decode(inout self, output_tokens: List[Int64]) -> String: + def decode(mut self, output_tokens: List[Int64]) -> String: """Decodes a string by indexing the vocabulary.""" decoded = String() for token_id in output_tokens: diff --git "a/examples/graph-api/pipelines/llama2/tokenizer/max_heap.\360\237\224\245" "b/examples/graph-api/pipelines/llama2/tokenizer/max_heap.\360\237\224\245" index a91dd1ab..6898fe81 100644 --- "a/examples/graph-api/pipelines/llama2/tokenizer/max_heap.\360\237\224\245" +++ "b/examples/graph-api/pipelines/llama2/tokenizer/max_heap.\360\237\224\245" @@ -62,12 +62,12 @@ struct MaxHeap[ElementType: OrderedElement, reverse: Bool = False]( """Checks whether the heap has any elements in it.""" return len(self) != 0 - fn push(inout self, owned elem: ElementType): + fn push(mut self, owned elem: ElementType): """Adds a value to the heap.""" self.heap.append(elem^) self._bubble_up(len(self.heap) - 1) - fn pop(inout self) -> ElementType: + fn pop(mut self) -> ElementType: """Removes the top element from the heap and return it.""" debug_assert(bool(self), "heap is empty") @@ -76,7 +76,7 @@ struct MaxHeap[ElementType: OrderedElement, reverse: Bool = False]( self._sink_down(0) return top^ - fn _bubble_up(inout self, idx: Int): + fn _bubble_up(mut self, idx: Int): if idx == 0: return @@ -89,7 +89,7 @@ struct MaxHeap[ElementType: OrderedElement, reverse: Bool = False]( ) self._bubble_up(parent_idx) - fn _sink_down(inout self, idx: Int): + fn _sink_down(mut self, idx: Int): var li = _left_child_idx(idx) var ri = _right_child_idx(idx) diff --git "a/examples/graph-api/pipelines/llama3/kv_cache.\360\237\224\245" "b/examples/graph-api/pipelines/llama3/kv_cache.\360\237\224\245" index 7e358e1d..cfb30753 100644 --- "a/examples/graph-api/pipelines/llama3/kv_cache.\360\237\224\245" +++ "b/examples/graph-api/pipelines/llama3/kv_cache.\360\237\224\245" @@ -32,7 +32,7 @@ struct KVCache: var sequence_length: Int def __init__( - inout self, + mut self, hp: LlamaHParams, max_length: Int, batch_size: Int, @@ -51,7 +51,7 @@ struct KVCache: self.sequence_length = 0 - def update(inout self, owned keys: AnyMemory, owned values: AnyMemory): + def update(mut self, owned keys: AnyMemory, owned values: AnyMemory): """Updates the KV Cache with data from new tokens.""" cpu_device = self.keys.device() keys_tensor = keys^.to_device_tensor().move_to(cpu_device) diff --git "a/examples/graph-api/pipelines/llama3/model/llama.\360\237\224\245" "b/examples/graph-api/pipelines/llama3/model/llama.\360\237\224\245" index cbc0ec15..707897c9 100644 --- "a/examples/graph-api/pipelines/llama3/model/llama.\360\237\224\245" +++ "b/examples/graph-api/pipelines/llama3/model/llama.\360\237\224\245" @@ -72,7 +72,7 @@ struct Llama3_NaiveKVCache[encoding: QuantizationEncoding = Float32Encoding]: def __init__(out self, model_path: Path): self.model = GGUFFile(model_path) - def build_graph(inout self, name: String) -> Graph: + def build_graph(mut self, name: String) -> Graph: params = self.hyperparams() alias model_dtype = DType.bfloat16 if encoding.id() == BFloat16Encoding.id() else DType.float32 cache_type = TensorType( @@ -224,7 +224,7 @@ struct Llama3[ def __init__(out self, model_path: Path): self.model = GGUFFile(model_path) - def build_graph(inout self, name: String) -> Graph: + def build_graph(mut self, name: String) -> Graph: params = self.hyperparams() cache_type = OpaqueType( ContiguousKVCacheCollection[type, kv_params].id() diff --git "a/examples/graph-api/pipelines/llama3/run.\360\237\224\245" "b/examples/graph-api/pipelines/llama3/run.\360\237\224\245" index 458dad57..9aecac4b 100644 --- "a/examples/graph-api/pipelines/llama3/run.\360\237\224\245" +++ "b/examples/graph-api/pipelines/llama3/run.\360\237\224\245" @@ -95,7 +95,7 @@ struct Config: if encoding[String] == "bfloat16": raise "bfloat16 is not currently supported on ARM" - fn get(inout self, key: String) raises -> OptionValue: + fn get(mut self, key: String) raises -> OptionValue: """Returns an option value for `key` in the underlying config. Args: @@ -109,7 +109,7 @@ struct Config: """ return self.config[key] - fn set(inout self, key: String, val: OptionValue): + fn set(mut self, key: String, val: OptionValue): """Sets a new value for a given config key. This will overwrite the old value if the key is already present. @@ -143,7 +143,7 @@ def _get_attention_mask( def _generation_loop[ type: DType, kv_params: KVCacheStaticParams ]( - inout metrics: Metrics, + mut metrics: Metrics, n_layers: Int, compiled_model: Model, tokenizer: TikTokenEncoder, @@ -254,7 +254,7 @@ def generate_text_naive( compiled_model: Model, params: LlamaHParams, config: Config, - inout metrics: Metrics, + mut metrics: Metrics, execution_device: Device, use_gpu: Bool, ): @@ -393,7 +393,7 @@ def generate_text[ compiled_model: Model, params: LlamaHParams, config: Config, - inout metrics: Metrics, + mut metrics: Metrics, execution_device: Device, use_gpu: Bool, ): diff --git "a/examples/graph-api/pipelines/metrics/metrics.\360\237\224\245" "b/examples/graph-api/pipelines/metrics/metrics.\360\237\224\245" index 687e13c3..069ca8b9 100644 --- "a/examples/graph-api/pipelines/metrics/metrics.\360\237\224\245" +++ "b/examples/graph-api/pipelines/metrics/metrics.\360\237\224\245" @@ -52,11 +52,11 @@ struct Metrics: self.traces.append(Trace[TraceLevel.OP]("PipelineMetric")) self.traces[0].start() - def set_tokens_in_prompt(inout self, tokens_in_prompt: Int): + def set_tokens_in_prompt(mut self, tokens_in_prompt: Int): """Provides the count of tokens processed in the prompt.""" self.tokens_in_prompt = tokens_in_prompt - def begin_timing_startup(inout self): + def begin_timing_startup(mut self): """Begins measurement of the pipeline startup time.""" self.start_startup = monotonic() self.traces.append( @@ -64,41 +64,41 @@ struct Metrics: ) self.traces[-1].start() - def end_timing_startup(inout self): + def end_timing_startup(mut self): """Ends measurement of the pipeline startup time.""" self.end_startup = monotonic() self.traces[-1].end() _ = self.traces.pop() - def begin_timing_prompt(inout self): + def begin_timing_prompt(mut self): """Begins timing from before prompt processing.""" self.start_time_before_prompt = monotonic() - def begin_timing_warmup(inout self): + def begin_timing_warmup(mut self): """Begins timing from before an optional warmup run.""" if not self.start_startup or self.end_startup: raise "Error: Warmup should be included within startup time" self.start_time_before_warmup = monotonic() - def end_timing_warmup(inout self): + def end_timing_warmup(mut self): """Ends measurement of an optional warmup run.""" if not self.start_startup or self.end_startup: raise "Error: Warmup should be included within startup time" self.end_warmup = monotonic() - def begin_timing_tokenization(inout self): + def begin_timing_tokenization(mut self): """Begins timing from before tokenization.""" if not self.start_time_before_prompt or self.start_time_before_context: raise "Error: Tokenization should be included within TTFT" self.start_time_before_tokenization = monotonic() - def end_timing_tokenization(inout self): + def end_timing_tokenization(mut self): """Ends measurement of tokenization.""" if not self.start_time_before_prompt or self.start_time_before_context: raise "Error: Tokenization should be included within TTFT" self.end_tokenization = monotonic() - def begin_timing_generation(inout self): + def begin_timing_generation(mut self): """Begins timing from the first generated token.""" self.start_time_before_generation = monotonic() self.traces.append( @@ -110,7 +110,7 @@ struct Metrics: ) self.traces[-1].start() - def new_token(inout self): + def new_token(mut self): """Increments the total tokens generated and corresponding metrics.""" if not self.start_time_before_context: # If this is the first token, store the current time for reporting @@ -133,7 +133,7 @@ struct Metrics: ) self.traces[-1].start() - def end_timing(inout self): + def end_timing(mut self): """Ends timing token generation.""" self.end_time = monotonic() for trace in self.traces: diff --git "a/examples/graph-api/pipelines/nn/transformer.\360\237\224\245" "b/examples/graph-api/pipelines/nn/transformer.\360\237\224\245" index 8d9f01a2..1a14d446 100644 --- "a/examples/graph-api/pipelines/nn/transformer.\360\237\224\245" +++ "b/examples/graph-api/pipelines/nn/transformer.\360\237\224\245" @@ -87,7 +87,7 @@ struct Transformer[model_dtype: DType = DType.float32]: var rope_scaling: Optional[Symbol] def __init__( - inout self, + mut self, dim: Int, n_heads: Int, embedding: Embedding, diff --git "a/examples/graph-api/pipelines/quantize_tinystories/load_tinystories.\360\237\224\245" "b/examples/graph-api/pipelines/quantize_tinystories/load_tinystories.\360\237\224\245" index db85484f..95287311 100644 --- "a/examples/graph-api/pipelines/quantize_tinystories/load_tinystories.\360\237\224\245" +++ "b/examples/graph-api/pipelines/quantize_tinystories/load_tinystories.\360\237\224\245" @@ -94,7 +94,7 @@ struct TeenyTinyLlama[encoding: QuantizationEncoding]: # Read Llama hyperparameters from the checkpoint. self.hyperparams = read_hyperparams_from_dict(self.quantized_params) - def build(inout self) -> Graph: + def build(mut self) -> Graph: """Build the Llama 2 graph using the quantized weights from checkpoint. """ # Set the KV cache and tokens input types. diff --git "a/examples/graph-api/pipelines/quantize_tinystories/quantize_tinystories.\360\237\224\245" "b/examples/graph-api/pipelines/quantize_tinystories/quantize_tinystories.\360\237\224\245" index 4b075bd6..7ebcfbd0 100644 --- "a/examples/graph-api/pipelines/quantize_tinystories/quantize_tinystories.\360\237\224\245" +++ "b/examples/graph-api/pipelines/quantize_tinystories/quantize_tinystories.\360\237\224\245" @@ -52,7 +52,7 @@ def param_key(name: String, layer_idx: Optional[Int] = None) -> String: def add_hyperparams_to_dict( - inout tensor_dict: TensorDict, hyperparams: LlamaHParams + mut tensor_dict: TensorDict, hyperparams: LlamaHParams ): """Copies all hyperparameters into a TensorDict for later checkpointing.""" tensor_dict.set( @@ -108,7 +108,7 @@ struct TeenyTinyLlama[ self.quantized_params = TensorDict() add_hyperparams_to_dict(self.quantized_params, self.hyperparams) - def build(inout self) -> Graph: + def build(mut self) -> Graph: """Build the Llama 2 graph, quantizing its weights by construction.""" # Set the KV cache and tokens input types. params = self.params_file.hyperparams() diff --git a/examples/graph-api/pipelines/replit/bpe_tokenizer/ball.mojo b/examples/graph-api/pipelines/replit/bpe_tokenizer/ball.mojo index 67570f88..c5d06b7c 100644 --- a/examples/graph-api/pipelines/replit/bpe_tokenizer/ball.mojo +++ b/examples/graph-api/pipelines/replit/bpe_tokenizer/ball.mojo @@ -62,7 +62,7 @@ struct Ball[T: CollectionElement]: """Checks whether the node is still in the list.""" return 0 <= id < len(self._arena) and self._arena[id] - fn append(inout self, owned value: T) -> Self.ID: + fn append(mut self, owned value: T) -> Self.ID: """Adds a new element to the back of the list.""" var id = len(self._arena) var node = Node[T](value^, self._tail, None) @@ -74,7 +74,7 @@ struct Ball[T: CollectionElement]: self._arena.append(node) return id - fn remove(inout self, id: Self.ID): + fn remove(mut self, id: Self.ID): """Removes an element from the list.""" var node = self._arena[id].value() self._arena[id] = None diff --git a/examples/graph-api/pipelines/replit/bpe_tokenizer/json.mojo b/examples/graph-api/pipelines/replit/bpe_tokenizer/json.mojo index bd844683..89189a4e 100644 --- a/examples/graph-api/pipelines/replit/bpe_tokenizer/json.mojo +++ b/examples/graph-api/pipelines/replit/bpe_tokenizer/json.mojo @@ -159,7 +159,7 @@ struct TokenType(EqualityComparable, Stringable, KeyElement): raise "Cannot convert token type " + str(self) + " into a NodeType." -def get_next_token(inout s: StringRef) -> (StringRef, TokenType): +def get_next_token(mut s: StringRef) -> (StringRef, TokenType): """Gets the next token within the limits and returns the unscanned indices. Args: @@ -357,7 +357,7 @@ struct JsonStorage: return self.get(args_list) -def _from_string(inout s: StringRef) -> JsonStorage: +def _from_string(mut s: StringRef) -> JsonStorage: # Dict and Arrays will want the entire span as their location. orig_buffer = s diff --git a/examples/graph-api/pipelines/replit/bpe_tokenizer/max_heap.mojo b/examples/graph-api/pipelines/replit/bpe_tokenizer/max_heap.mojo index 9bc4eb7d..ec9d9d1d 100644 --- a/examples/graph-api/pipelines/replit/bpe_tokenizer/max_heap.mojo +++ b/examples/graph-api/pipelines/replit/bpe_tokenizer/max_heap.mojo @@ -59,12 +59,12 @@ struct MaxHeap[ElementType: OrderableElement](Sized, Boolable): """Checks whether the heap has any elements in it.""" return len(self) != 0 - fn push(inout self, owned elem: ElementType): + fn push(mut self, owned elem: ElementType): """Adds a value to the heap.""" self.heap.append(elem^) self._bubble_up(len(self.heap) - 1) - fn pop(inout self) -> ElementType: + fn pop(mut self) -> ElementType: """Removes the top element from the heap and return it.""" debug_assert(bool(self), "heap is empty") self._sink_down(self.begin_idx) @@ -73,10 +73,10 @@ struct MaxHeap[ElementType: OrderableElement](Sized, Boolable): self.begin_idx += 1 return top - fn _swap(inout self, i1: Int, i2: Int): + fn _swap(mut self, i1: Int, i2: Int): self.heap.swap_elements(i1, i2) - fn _bubble_up(inout self, idx: Int): + fn _bubble_up(mut self, idx: Int): if idx == self.begin_idx: return @@ -87,7 +87,7 @@ struct MaxHeap[ElementType: OrderableElement](Sized, Boolable): self._swap(parent_idx, idx) self._bubble_up(parent_idx) - fn _sink_down(inout self, idx: Int): + fn _sink_down(mut self, idx: Int): var li = self._left_child_idx(idx) var ri = self._right_child_idx(idx) diff --git a/examples/graph-api/pipelines/replit/config.mojo b/examples/graph-api/pipelines/replit/config.mojo index 37ec9a5c..5869be1f 100644 --- a/examples/graph-api/pipelines/replit/config.mojo +++ b/examples/graph-api/pipelines/replit/config.mojo @@ -66,7 +66,7 @@ struct ReplitConfigRegistry(ConfigRegistry): var registry: ConfigRegistryDict def __init__( - inout self, + mut self, additional_pipeline_args: Optional[ConfigRegistryDict] = None, ): """ diff --git a/examples/graph-api/pipelines/replit/model/replit.mojo b/examples/graph-api/pipelines/replit/model/replit.mojo index 8aa90c3e..6092110b 100644 --- a/examples/graph-api/pipelines/replit/model/replit.mojo +++ b/examples/graph-api/pipelines/replit/model/replit.mojo @@ -51,7 +51,7 @@ struct Replit[T: LoadableModel, dtype: DType, kv_params: KVCacheStaticParams]: def build_graph( self, - inout params: T, + mut params: T, name: String, ) -> Graph: """Builds the replit model graph. diff --git a/examples/graph-api/pipelines/replit/run.mojo b/examples/graph-api/pipelines/replit/run.mojo index c1566d85..bdfd320c 100644 --- a/examples/graph-api/pipelines/replit/run.mojo +++ b/examples/graph-api/pipelines/replit/run.mojo @@ -73,7 +73,7 @@ struct Config: var dtype: DType def __init__( - inout self, + mut self, additional_pipeline_args: Optional[ConfigRegistryDict] = None, additional_defaults: Optional[Dict[String, OptionValue]] = None, ): @@ -117,7 +117,7 @@ struct Config: def __contains__(self, key: String): return key in self.config - fn get(inout self, key: String) raises -> OptionValue: + fn get(mut self, key: String) raises -> OptionValue: """Returns an option value for `key` in the underlying config. Args: @@ -134,7 +134,7 @@ struct Config: except: raise "KeyError: " + key - fn set(inout self, key: String, val: OptionValue): + fn set(mut self, key: String, val: OptionValue): """Sets a new value for a given config key. This will overwrite the old value if the key is already present. @@ -211,7 +211,7 @@ struct ReplitPipeline[dtype: DType, kv_params: KVCacheStaticParams]: """If non-zero, pad input sequence to nearest multiple of given value.""" def __init__( - inout self, + mut self, model_path: Path, use_gpu: Bool = False, max_length: Optional[Int] = None, @@ -328,7 +328,7 @@ struct ReplitPipeline[dtype: DType, kv_params: KVCacheStaticParams]: else: return DEFAULT_MAX_SEQ_LEN - def reset(inout self, prompt: String) -> Int: + def reset(mut self, prompt: String) -> Int: """Resets the prompt and model state.""" self._initial_prompt = prompt self._max_seq_len = self._get_max_tokens(len(prompt)) @@ -362,17 +362,17 @@ struct ReplitPipeline[dtype: DType, kv_params: KVCacheStaticParams]: self._is_end_of_text = False return encoded_prompt.size - def next_token(inout self) -> Optional[String]: + def next_token(mut self) -> Optional[String]: """Generates the next token, or None if the end has been reached.""" return self.next_token(WeightedSampler(0)) - def _set_next_token_tensor(inout self, owned next_token_tensor: AnyTensor): + def _set_next_token_tensor(mut self, owned next_token_tensor: AnyTensor): """Set the given value as next token tensor. If the chosen device is gpu, value will be copied over to the device.""" self._next_token_tensor = next_token_tensor^ - def _get_attention_mask(inout self) -> AnyTensor: + def _get_attention_mask(mut self) -> AnyTensor: """Generates attention mask for current input sequence. Result is placed on the chosen device. """ @@ -389,7 +389,7 @@ struct ReplitPipeline[dtype: DType, kv_params: KVCacheStaticParams]: def next_token[ Sampler: TokenSampler - ](inout self, sampler: Sampler) -> Optional[String]: + ](mut self, sampler: Sampler) -> Optional[String]: """Generates the next token, or None if the end has been reached.""" if not self._seq_ids: raise "KV Cache not initialized, you must call `reset` before calling `next_token`" diff --git a/examples/graph-api/pipelines/replit/tokenizer/autotokenizer.mojo b/examples/graph-api/pipelines/replit/tokenizer/autotokenizer.mojo index d8e938c2..c8769d9d 100644 --- a/examples/graph-api/pipelines/replit/tokenizer/autotokenizer.mojo +++ b/examples/graph-api/pipelines/replit/tokenizer/autotokenizer.mojo @@ -180,7 +180,7 @@ struct AutoTokenizer(Tokenizer): return token_ids_list, attn_mask_list - def decode(inout self, output_tokens: List[Int64]) -> String: + def decode(mut self, output_tokens: List[Int64]) -> String: """Decodes tokens using the autotokenizer and accounts for spaces.""" # Attempt to produce correct output in a streaming setting. diff --git a/examples/graph-api/pipelines/replit/tokenizer/tokenizer.mojo b/examples/graph-api/pipelines/replit/tokenizer/tokenizer.mojo index 339343fc..053f8391 100644 --- a/examples/graph-api/pipelines/replit/tokenizer/tokenizer.mojo +++ b/examples/graph-api/pipelines/replit/tokenizer/tokenizer.mojo @@ -29,5 +29,5 @@ trait Tokenizer(Movable): ) -> (List[Int64], List[Int64]): ... - def decode(inout self, output_tokens: List[Int64]) -> String: + def decode(mut self, output_tokens: List[Int64]) -> String: ... diff --git "a/examples/graph-api/pipelines/tokenizer/arena_linked_list.\360\237\224\245" "b/examples/graph-api/pipelines/tokenizer/arena_linked_list.\360\237\224\245" index a9e3c3f5..ca1495df 100644 --- "a/examples/graph-api/pipelines/tokenizer/arena_linked_list.\360\237\224\245" +++ "b/examples/graph-api/pipelines/tokenizer/arena_linked_list.\360\237\224\245" @@ -62,7 +62,7 @@ struct ArenaLinkedList[ElementType: CollectionElement]: """Checks whether the node is still in the list.""" return 0 <= id < len(self._arena) and self.node(id) - def append(inout self, owned value: ElementType) -> Self.ID: + def append(mut self, owned value: ElementType) -> Self.ID: """Adds a new element to the back of the list.""" id = len(self._arena) node = Node[ElementType](value^, self._tail, None) @@ -74,7 +74,7 @@ struct ArenaLinkedList[ElementType: CollectionElement]: self._arena.append(node) return id - def remove(inout self, id: Self.ID): + def remove(mut self, id: Self.ID): """Removes an element from the list.""" debug_assert(bool(self.node(id)), "removing item not in list") debug_assert(bool(self._head), "removing from empty list") diff --git "a/examples/graph-api/pipelines/tokenizer/autotokenizer.\360\237\224\245" "b/examples/graph-api/pipelines/tokenizer/autotokenizer.\360\237\224\245" index 552b7686..88963670 100644 --- "a/examples/graph-api/pipelines/tokenizer/autotokenizer.\360\237\224\245" +++ "b/examples/graph-api/pipelines/tokenizer/autotokenizer.\360\237\224\245" @@ -153,7 +153,7 @@ struct AutoTokenizer(Tokenizer): return result - def decode(inout self, output_tokens: List[Int64]) -> String: + def decode(mut self, output_tokens: List[Int64]) -> String: """Decodes tokens using the autotokenizer and accounts for spaces.""" # Attempt to produce correct output in a streaming setting. diff --git "a/examples/graph-api/pipelines/tokenizer/bpe.\360\237\224\245" "b/examples/graph-api/pipelines/tokenizer/bpe.\360\237\224\245" index ebdabb53..904b50b1 100644 --- "a/examples/graph-api/pipelines/tokenizer/bpe.\360\237\224\245" +++ "b/examples/graph-api/pipelines/tokenizer/bpe.\360\237\224\245" @@ -39,7 +39,7 @@ struct _SplitIter[is_mutable: Bool, origin: Origin[is_mutable].type]: fn __iter__(self) -> Self: return self - fn __next__(inout self) -> StringSlice[origin]: + fn __next__(mut self) -> StringSlice[origin]: """Return the next split slice via StringRef.find().""" # If we've already split `max` times, return the final slice. if self.max and self.max.value() == 0: @@ -138,7 +138,7 @@ struct MergeOption(OrderedElement): ) -def read[T: CollectionElement](inout span: Span[Byte, _]) -> T: +def read[T: CollectionElement](mut span: Span[Byte, _]) -> T: """Read a binary type out of a byte buffer and increment the pointer.""" value = span.unsafe_ptr().bitcast[T]()[] span = span[sizeof[T]() :] @@ -240,7 +240,7 @@ struct BPETokenizer: with open(path, "r") as file: return Self.from_binary(file.read()) - def add_token(inout self, token: String, score: Float32): + def add_token(mut self, token: String, score: Float32): """Add a token to the vocabulary.""" if token not in self.token_ids: self.token_ids[token] = len(self.vocab) diff --git "a/examples/graph-api/pipelines/tokenizer/max_heap.\360\237\224\245" "b/examples/graph-api/pipelines/tokenizer/max_heap.\360\237\224\245" index 3482482c..32b3a5cd 100644 --- "a/examples/graph-api/pipelines/tokenizer/max_heap.\360\237\224\245" +++ "b/examples/graph-api/pipelines/tokenizer/max_heap.\360\237\224\245" @@ -62,12 +62,12 @@ struct MaxHeap[ElementType: OrderedElement, reverse: Bool = False]( """Checks whether the heap has any elements in it.""" return len(self) != 0 - fn push(inout self, owned elem: ElementType): + fn push(mut self, owned elem: ElementType): """Adds a value to the heap.""" self.heap.append(elem^) self._bubble_up(len(self.heap) - 1) - fn pop(inout self) -> ElementType: + fn pop(mut self) -> ElementType: """Removes the top element from the heap and return it.""" debug_assert(bool(self), "heap is empty") @@ -76,7 +76,7 @@ struct MaxHeap[ElementType: OrderedElement, reverse: Bool = False]( self._sink_down(0) return top^ - fn _bubble_up(inout self, idx: Int): + fn _bubble_up(mut self, idx: Int): if idx == 0: return @@ -89,7 +89,7 @@ struct MaxHeap[ElementType: OrderedElement, reverse: Bool = False]( ) self._bubble_up(parent_idx) - fn _sink_down(inout self, idx: Int): + fn _sink_down(mut self, idx: Int): var li = _left_child_idx(idx) var ri = _right_child_idx(idx) diff --git a/examples/graph-api/pipelines/tokenizer/regex.mojo b/examples/graph-api/pipelines/tokenizer/regex.mojo index ab8a3407..7eb9324f 100644 --- a/examples/graph-api/pipelines/tokenizer/regex.mojo +++ b/examples/graph-api/pipelines/tokenizer/regex.mojo @@ -46,7 +46,7 @@ fn llvm_regcomp(ptr: UnsafePointer[_CRegex], pattern: String, mode: Int) -> Int: fn llvm_regexec( ptr: UnsafePointer[_CRegex], string: String, - inout pmatch: List[_CRegexMatch], + mut pmatch: List[_CRegexMatch], mode: Int, ) -> Int: return MLIR_func["llvm_regexec", Int]( @@ -98,7 +98,7 @@ struct _CRegex: self[]._compile(pattern, options | CompileOption.EXTENDED) return self - def _compile(inout self, pattern: String, options: Int): + def _compile(mut self, pattern: String, options: Int): err = llvm_regcomp(self._ptr(), pattern, options) if err: raise self._error(err) @@ -163,7 +163,7 @@ struct _MatchIter[ var negative_lookahead_hack: Bool def __init__( - inout self, + mut self, regex: Pointer[Regex, regex_origin], string: Pointer[String, string_origin], negative_lookahead_hack: Bool = False, @@ -178,7 +178,7 @@ struct _MatchIter[ fn __iter__(self) -> Self: return self - def __next__(inout self) -> Match[string_origin]: + def __next__(mut self) -> Match[string_origin]: m = self.next_match.value() self._next() return m^ @@ -189,7 +189,7 @@ struct _MatchIter[ fn __has_next__(self) -> Bool: return self.__len__() > 0 - def _next(inout self): + def _next(mut self): m = self.regex[].find(self.string[], start=self.start) self.next_match = m if m and self.negative_lookahead_hack: @@ -220,7 +220,7 @@ struct Match[origin: ImmutableOrigin](Writable): fn __str__(self) -> String: return str(self[0]) - fn write_to[W: Writer](self, inout writer: W): + fn write_to[W: Writer](self, mut writer: W): # TODO: Avoid intermediate String allocation. writer.write(str(self)) diff --git a/examples/graph-api/pipelines/tokenizer/tiktoken.mojo b/examples/graph-api/pipelines/tokenizer/tiktoken.mojo index a6fbcc01..504f9464 100644 --- a/examples/graph-api/pipelines/tokenizer/tiktoken.mojo +++ b/examples/graph-api/pipelines/tokenizer/tiktoken.mojo @@ -22,7 +22,7 @@ from ..weights.gguf import GGUFArray, GGUFString from . import Tokenizer -def _next_rune(inout span: Span[Byte, _]) -> Int: +def _next_rune(mut span: Span[Byte, _]) -> Int: if not span[0] & 0x80: result = int(span[0]) span = span[1:] @@ -88,7 +88,7 @@ struct TikTokenEncoder(Tokenizer): var special_tokens: Dict[String, Int] def __init__( - inout self, + mut self, owned bpe: BPETokenizer, owned regex: Regex, owned special_tokens: Dict[String, Int], @@ -169,7 +169,7 @@ struct TikTokenEncoder(Tokenizer): return special_id.value() return self.bpe.token_ids[string] - def decode(inout self, output_tokens: List[Int64]) -> String: + def decode(mut self, output_tokens: List[Int64]) -> String: decoded = String() for token_id in output_tokens: decoded += self.bpe.vocab[int(token_id[])].token diff --git "a/examples/graph-api/pipelines/tokenizer/tokenizer.\360\237\224\245" "b/examples/graph-api/pipelines/tokenizer/tokenizer.\360\237\224\245" index 718690ad..43376e37 100644 --- "a/examples/graph-api/pipelines/tokenizer/tokenizer.\360\237\224\245" +++ "b/examples/graph-api/pipelines/tokenizer/tokenizer.\360\237\224\245" @@ -27,5 +27,5 @@ trait Tokenizer(Movable): ) -> List[Int64]: ... - def decode(inout self, output_tokens: List[Int64]) -> String: + def decode(mut self, output_tokens: List[Int64]) -> String: ... diff --git "a/examples/graph-api/pipelines/weights/ggml_quants.\360\237\224\245" "b/examples/graph-api/pipelines/weights/ggml_quants.\360\237\224\245" index 8f044577..5606166f 100644 --- "a/examples/graph-api/pipelines/weights/ggml_quants.\360\237\224\245" +++ "b/examples/graph-api/pipelines/weights/ggml_quants.\360\237\224\245" @@ -33,7 +33,7 @@ struct BlockQ40: """Nibbles / quants.""" def __init__( - inout self, + mut self, d: Float16, qs: InlineArray[UInt8, Self.QK4_0 // 2], ): @@ -66,7 +66,7 @@ struct BlockQ80: """Quants.""" def __init__( - inout self, + mut self, d: Float16, qs: InlineArray[Int8, Self.QK8_0], ): @@ -115,7 +115,7 @@ struct BlockQ4K: """4-bit quants.""" def __init__( - inout self, + mut self, d: Float16, dmin: Float16, scales: InlineArray[UInt8, K_SCALE_SIZE], @@ -160,7 +160,7 @@ struct BlockQ6K: """Super-block scale.""" def __init__( - inout self, + mut self, ql: InlineArray[UInt8, QK_K // 2], qh: InlineArray[UInt8, QK_K // 4], scales: InlineArray[Int8, QK_K // 16], diff --git "a/examples/graph-api/pipelines/weights/gguf.\360\237\224\245" "b/examples/graph-api/pipelines/weights/gguf.\360\237\224\245" index 281d62ae..5de5f6cd 100644 --- "a/examples/graph-api/pipelines/weights/gguf.\360\237\224\245" +++ "b/examples/graph-api/pipelines/weights/gguf.\360\237\224\245" @@ -617,7 +617,7 @@ struct GGUFReader: self.f = f^ @always_inline - fn align_to(inout self, alignment: Int) raises -> None: + fn align_to(mut self, alignment: Int) raises -> None: var overshoot = self.offset % alignment if overshoot == 0: return @@ -625,31 +625,31 @@ struct GGUFReader: self.seek(alignment - overshoot) @always_inline - fn read_bytes(inout self, num_bytes: Int) raises -> Tensor[DType.uint8]: + fn read_bytes(mut self, num_bytes: Int) raises -> Tensor[DType.uint8]: self.offset += num_bytes return self.f.read_bytes(num_bytes) @always_inline - fn seek(inout self, num_bytes: Int) raises: + fn seek(mut self, num_bytes: Int) raises: self.offset += num_bytes _ = self.f.seek(num_bytes) @always_inline - fn dtype_element[type: DType](inout self) raises -> Scalar[type]: + fn dtype_element[type: DType](mut self) raises -> Scalar[type]: var bytes_tensor: Tensor[DType.uint8] = self.read_bytes( sizeof[Scalar[type]]() ) return bytes_tensor.unsafe_ptr().bitcast[Scalar[type]]().load() @always_inline - fn gguf_string(inout self) raises -> GGUFString: + fn gguf_string(mut self) raises -> GGUFString: var n = int(self.dtype_element[DType.uint64]()) var key_data: Tensor[DType.uint8] = self.read_bytes(n) return GGUFString( n, key_data._steal_ptr().bitcast[Scalar[DType.uint8]]() ) - fn gguf_kv(inout self) raises -> GGUFKV: + fn gguf_kv(mut self) raises -> GGUFKV: @always_inline @parameter fn _gguf_value[type: DType]() raises -> GGUFValue: @@ -705,7 +705,7 @@ struct GGUFReader: # Dispatch on dtype. return GGUFKV(key, kv_type.dispatch[GGUFValue, _gguf_value]()) - fn gguf_tensor_info(inout self) raises -> GGUFTensorInfo: + fn gguf_tensor_info(mut self) raises -> GGUFTensorInfo: var name = self.gguf_string() var n_dims = self.dtype_element[DType.uint32]() @@ -848,9 +848,9 @@ struct GGUFFile(LoadableModel): fn get[ type: DType - ]( - inout self, key: String, layer_idx: Optional[Int] = None - ) raises -> Tensor[type]: + ](mut self, key: String, layer_idx: Optional[Int] = None) raises -> Tensor[ + type + ]: var full_key = key + ".weight" if layer_idx: full_key = "blk." + str(layer_idx.value()) + "." + full_key diff --git "a/examples/graph-api/pipelines/weights/llama2checkpoint.\360\237\224\245" "b/examples/graph-api/pipelines/weights/llama2checkpoint.\360\237\224\245" index 71841f3c..5227eac5 100644 --- "a/examples/graph-api/pipelines/weights/llama2checkpoint.\360\237\224\245" +++ "b/examples/graph-api/pipelines/weights/llama2checkpoint.\360\237\224\245" @@ -206,9 +206,9 @@ struct LlamaCFile(LoadableModel): fn get[ type: DType - ]( - inout self, key: String, layer_idx: Optional[Int] = None - ) raises -> Tensor[type]: + ](mut self, key: String, layer_idx: Optional[Int] = None) raises -> Tensor[ + type + ]: # Heap allocates and copies output, which is owned by the caller. var tensor_ref: TensorRef[type] if key == "token_embd": diff --git "a/examples/graph-api/pipelines/weights/loadable_model.\360\237\224\245" "b/examples/graph-api/pipelines/weights/loadable_model.\360\237\224\245" index e0a1dd7d..07d2cd3a 100644 --- "a/examples/graph-api/pipelines/weights/loadable_model.\360\237\224\245" +++ "b/examples/graph-api/pipelines/weights/loadable_model.\360\237\224\245" @@ -66,12 +66,12 @@ trait LoadableModel(Movable): fn get[ type: DType - ]( - inout self, key: String, layer_idx: Optional[Int] = None - ) raises -> Tensor[type]: + ](mut self, key: String, layer_idx: Optional[Int] = None) raises -> Tensor[ + type + ]: """Returns a tensor for `key` at layer `layer_idx`, possibly seeking the file. - `self` is `inout` here due to implementations that seek a file pointer. + `self` is `mut` here due to implementations that seek a file pointer. Args: key: Used to look up the tensor in the weights file. diff --git a/examples/graph-api/test/configs/test_parse_args.mojo b/examples/graph-api/test/configs/test_parse_args.mojo index dd9f7425..cabfd91c 100644 --- a/examples/graph-api/test/configs/test_parse_args.mojo +++ b/examples/graph-api/test/configs/test_parse_args.mojo @@ -34,7 +34,7 @@ struct DummyConfigRegistry(ConfigRegistry): var registry: ConfigRegistryDict def __init__( - inout self, + mut self, additional_pipeline_args: ConfigRegistryDict = ConfigRegistryDict(), ): self.registry = ConfigRegistryDict() diff --git a/examples/graph-api/test/llama3/test_layers.mojo b/examples/graph-api/test/llama3/test_layers.mojo index fe3ab54f..86b02469 100644 --- a/examples/graph-api/test/llama3/test_layers.mojo +++ b/examples/graph-api/test/llama3/test_layers.mojo @@ -106,7 +106,7 @@ struct NanoLlama(LoadableModel): fn get[ type: DType ]( - inout self, key: String, _layer_idx: Optional[Int] = None + mut self, key: String, _layer_idx: Optional[Int] = None ) raises -> Tensor[type]: constrained[type is DType.float32, "bork"]() return self.weights[key].astype[type]() diff --git a/examples/graph-api/test/replit/test_logits.mojo b/examples/graph-api/test/replit/test_logits.mojo index 10141eb5..12e1786c 100644 --- a/examples/graph-api/test/replit/test_logits.mojo +++ b/examples/graph-api/test/replit/test_logits.mojo @@ -171,12 +171,12 @@ struct TestCheckpoint(LoadableModel): fn get[ type: DType ]( - inout self, key: String, layer_idx: Optional[Int] = None + mut self, key: String, layer_idx: Optional[Int] = None ) raises -> Tensor[type]: """Returns a tensor for `key` at a given layer `layer_idx`, possibly seeking the file. - `self` is `inout` here due to implementations that seek a file pointer. + `self` is `mut` here due to implementations that seek a file pointer. Args: key: Used to look up the tensor in the weights file. diff --git "a/examples/inference/stable-diffusion-mojo-onnx/scheduler.\360\237\224\245" "b/examples/inference/stable-diffusion-mojo-onnx/scheduler.\360\237\224\245" index c8c7ab2a..11631b5e 100644 --- "a/examples/inference/stable-diffusion-mojo-onnx/scheduler.\360\237\224\245" +++ "b/examples/inference/stable-diffusion-mojo-onnx/scheduler.\360\237\224\245" @@ -75,7 +75,7 @@ struct Scheduler: self.alphas_cumprod.append(cumprod.cast[DType.float32]()) def step( - inout self, + mut self, model_output: FloatTensor, timestep: Int, sample: FloatTensor, @@ -126,7 +126,7 @@ struct Scheduler: return previous_sample def _get_previous_sample( - inout self, + mut self, sample: FloatTensor, timestep: Int, previous_timestep: Int,