Skip to content
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

[stdlib] Implement Int.from_bytes() and Int.as_bytes() #3768

Closed
wants to merge 71 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
71 commits
Select commit Hold shift + click to select a range
5cd4afc
Implement Int.from_bytes() function
msaelices Nov 12, 2024
c2e0047
Add entry to the changelog about Int.from_bytes()
msaelices Nov 12, 2024
1d785b4
Make the Int.from_bytes() accept an span of bytes instead of a list
msaelices Nov 12, 2024
58b5911
Fix import ordering
msaelices Nov 12, 2024
74b78c8
[stdlib] Move the Int.from_bytes() big_endian arg to a comptime param
msaelices Nov 13, 2024
37a5ac0
[Lit][CI] Exclude mojo files in examples subdirectories from lit (#3770)
Ahajha Nov 13, 2024
4a1057d
[CI][Lit] Ignore subdirectories of "examples" in lit config (#3771)
Ahajha Nov 13, 2024
db7d336
[stdlib] feat: Drop `Pythonable` requirement from argument check
ConnorGray Nov 12, 2024
1ed22c9
New Mojo manual page on operators and dunder methods
KenJones-Modular Nov 13, 2024
a2599b1
[******][GPU] Extend the address spaces to be GPU aware
abduld Nov 13, 2024
cdbb8a2
[stdlib] feat: Add CPython wrapper for `PyType_GenericAlloc()`
ConnorGray Nov 14, 2024
018634e
[examples] Add trailing slash to max-nightly conda channel for
Ahajha Nov 14, 2024
4119f71
Update lockfiles to point to latest nightly version: 24.6.0.dev202411…
modularbot Nov 14, 2024
3e78697
[******][GPU] Add a check if the target is AMD GPU
abduld Nov 14, 2024
2605c41
[******][GPU] Add the Float8e5m2 and Float8e4m3 scalar types
abduld Nov 14, 2024
df81ff6
[******][GPU] Make unsafe_pointer GPU agnostic
abduld Nov 14, 2024
9b2b4dc
Update lockfiles to point to latest nightly version: 24.6.0.dev202411…
modularbot Nov 14, 2024
3d53103
[External] [stdlib] Fix `math.math.mojo` examples (#50830)
soraros Nov 14, 2024
89309fb
[External] [stdlib] Clean up `b64encode` (2/N) (#50831)
soraros Nov 14, 2024
01a8db9
[External] [stdlib] Make `StringRef` Representable (#50840)
msaelices Nov 14, 2024
6a8f89f
[stdlib] Revert "Clean up b64encode (2/N)"
Ahajha Nov 15, 2024
b6e368b
[KGEN] Add a new POC for the accelerator arch
abduld Nov 15, 2024
513d725
[******][GPU] Revert address space changes introduced in #50730
abduld Nov 15, 2024
fba2784
Update lockfiles to point to latest nightly version: 24.6.0.dev202411…
modularbot Nov 15, 2024
863c734
[stdlib] feat: Add `_Global` interface for easier to use global
ConnorGray Nov 15, 2024
fa0cf05
[External] [stdlib] [NFC] Add docstring comment for `Span` slicing's …
modularbot Nov 15, 2024
6caa362
[******][GPU][AMD] Make WARP_SIZE accelerator agnostic
abduld Nov 15, 2024
4e06e05
[stdlib] Remove `UnsafePointer.bitcast` overload for `DType`
JoeLoser Nov 15, 2024
6a00715
Update lockfiles to point to latest nightly version: 24.6.0.dev202411…
modularbot Nov 16, 2024
cad47b5
[mojo-lang] Refactor some implicit conversion handling.
lattner Nov 16, 2024
60fe195
[External] [stdlib] Fix unsafe `String.write_bytes()` (#50972)
martinvuyk Nov 16, 2024
18b8c6d
[External] [stdlib] Deque - add remaining methods and tests (#50973)
avitkauskas Nov 16, 2024
4e34b67
[mojo-stdlib] Start phasing in some markers for `@implicit`
lattner Nov 16, 2024
66fd1f7
[******][GPU] Make compile_function aware of the target accelerator
abduld Nov 16, 2024
8046766
[stdlib] Allow passing variables to GPU `debug_assert`
jackos Nov 16, 2024
52ff607
Update lockfiles to point to latest nightly version: 24.6.0.dev202411…
modularbot Nov 17, 2024
90745e2
[mojo-lang] Allow `ref x: Type` in argument list.
lattner Nov 17, 2024
9c7581a
[mojo-stdlib] Use implicit `Self`, NFC.
lattner Nov 17, 2024
b1fe09d
[mojo-tools] Fix mojo-doc printing of ref args and results. (#51016)
lattner Nov 17, 2024
356b8ae
[stdlib] Add comparison to StringSlice for StringLiteral
lsh Nov 18, 2024
783b39d
[stdlib] Add implicit decorators to constructors, NFC
jackos Nov 18, 2024
91ca2c9
[mojo-lang] Turn on explicit by default constructors
jackos Nov 18, 2024
1a73e60
[docs] Add changelog fix for implicit conversions
jackos Nov 18, 2024
d99084e
Update lockfiles to point to latest nightly version: 24.6.0.dev202411…
modularbot Nov 18, 2024
9e90448
Implement Int.from_bytes() function
msaelices Nov 12, 2024
67f2e7f
[External] [stdlib] Clean up `b64encode` (2/N) (#50831)
soraros Nov 14, 2024
6b3aa36
[stdlib] Revert "Clean up b64encode (2/N)"
Ahajha Nov 15, 2024
f7dfa35
Update lockfiles to point to latest nightly version: 24.6.0.dev202411…
modularbot Nov 15, 2024
fd3f39b
[stdlib] feat: Add `_Global` interface for easier to use global
ConnorGray Nov 15, 2024
3846993
Update lockfiles to point to latest nightly version: 24.6.0.dev202411…
modularbot Nov 16, 2024
4cc1bb2
[mojo-lang] Refactor some implicit conversion handling.
lattner Nov 16, 2024
de63275
Update lockfiles to point to latest nightly version: 24.6.0.dev202411…
modularbot Nov 17, 2024
2a43fb3
[mojo-lang] Allow `ref x: Type` in argument list.
lattner Nov 17, 2024
3169d02
[mojo-stdlib] Use implicit `Self`, NFC.
lattner Nov 17, 2024
c18ccfe
[stdlib] Add implicit decorators to constructors, NFC
jackos Nov 18, 2024
7de77f6
[mojo-lang] Turn on explicit by default constructors
jackos Nov 18, 2024
1a13a67
[docs] Add changelog fix for implicit conversions
jackos Nov 18, 2024
5559d9e
Update lockfiles to point to latest nightly version: 24.6.0.dev202411…
modularbot Nov 18, 2024
bad047f
[stdlib] Implement Int.as_bytes()
msaelices Nov 21, 2024
ada3d2f
[stdlib] Update changelog to include Int.as_bytes()
msaelices Nov 21, 2024
1d28ad6
[External] [stdlib] Add full unicode support for character casing fun…
mzaks Nov 18, 2024
60a4154
[stdlib] Mark `Span` and `StringSlice` as register_passable trivial
lsh Nov 18, 2024
6788822
[stdlib] Public title
danteisalive Nov 19, 2024
d64b627
[stdlib] Fixes memory.mojo
kokrommy Nov 20, 2024
7024960
[stdlib] Added support for sin and cos on AMD GPUs.
fricc33 Nov 20, 2024
f2aab52
[stdlib] Change `CeilDivable` and `CeilDivableRaises` to just require
lsh Nov 20, 2024
6710f25
[stdlib] Makes sleep function more generic for GPUs.
kokrommy Nov 20, 2024
755bf66
Update lockfiles to point to latest nightly version: 24.6.0.dev202411…
modularbot Nov 20, 2024
1f73704
[mojo-lang] Refactor some implicit conversion handling.
lattner Nov 16, 2024
43fa507
[stdlib] Add implicit decorators to constructors, NFC
jackos Nov 18, 2024
26442f6
Merge branch 'nightly' into int-from-bytes
msaelices Nov 22, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .dict_benchmark.mojo.swp
Binary file not shown.
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# GitHub syntax highlighting
pixi.lock linguist-language=YAML linguist-generated=true
48 changes: 48 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "mojo-lldb",
"request": "launch",
"name": "Mojo: Debug current Mojo file",
"description": "Launch and debug the Mojo file that is active on the editor when the debug session starts.",
"mojoFile": "${file}",
"args": [],
"env": [],
"cwd": "${workspaceFolder}",
"runInTerminal": true
},
{
"type": "mojo-lldb",
"request": "launch",
"name": "Mojo: Debug Mojo file",
"description": "Launch and debug a Mojo file given its path.",
"mojoFile": "${workspaceFolder}/<your_file.mojo>",
"args": [],
"env": [],
"cwd": "${workspaceFolder}",
"runInTerminal": false
},
{
"type": "mojo-lldb",
"request": "launch",
"name": "Mojo: Debug binary",
"description": "Launch and debug a precompiled binary given its path.",
"program": "${workspaceFolder}/<your_binary>",
"args": [],
"env": [],
"cwd": "${workspaceFolder}",
"runInTerminal": false
},
{
"type": "mojo-lldb",
"request": "attach",
"name": "Mojo: Attach to process",
"description": "Search for a process to attach the debugger to.",
"pid": "${command:pickProcessToAttach}"
}
]
}
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"mojo.modularHomePath": "/home/msaelices/.modular"
}
4 changes: 4 additions & 0 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
* Fix TODO comment in test_representable function
* Implement Roundable __round__ with ndigits argument for all types
- https://github.com/modularml/mojo/issues/2679
- https://github.com/modularml/mojo/issues/2680
37 changes: 37 additions & 0 deletions add_docstring_to_cpython.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import re

def add_docstrings_to_mojo_file(mojo_filename, reference_filename, output_filename):
# Load reference mapping from the reference file
reference_mapping = {}
with open(reference_filename, 'r') as ref_file:
for line in ref_file:
match = re.match(r'(https://docs\.python\.org/3/c-api/.*#c\.\w+)\s+(\w+)', line)
if match:
reference_mapping[match.group(2)] = match.group(1)

# Open and process the Mojo file
with open(mojo_filename, 'r') as mojo_file:
lines = mojo_file.readlines()

updated_lines = []
for i, line in enumerate(lines):
updated_lines.append(line)
fn_match = re.match(r' fn (\w+)(.*)', line)
if fn_match:
function_name = fn_match.group(1)
if function_name in reference_mapping:
# Add the docstring below the function definition
docstring = f' """See {reference_mapping[function_name]}"""\n'
updated_lines.append(docstring)
else:
pass

# Write the updated lines to the output file
with open(output_filename, 'w') as output_file:
output_file.writelines(updated_lines)

# Example usage
mojo_filename = 'stdlib/src/python/_cpython.mojo'
reference_filename = 'c-abi.txt'
output_filename = 'stdlib/src/python/_cpython.mojo'
add_docstrings_to_mojo_file(mojo_filename, reference_filename, output_filename)
11 changes: 11 additions & 0 deletions bench_join.mojo
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from time import now

fn main():
l = List[String]()
for i in range(100_000):
l.append(str(i))
start = now()
s = ",".join(l)
#print(s)
end = now()
print('Len: ', len(s), 'Time: ', (end - start) / 1_000_000_000, 'seconds')
13 changes: 13 additions & 0 deletions bench_join.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from time import time as now

def main():
l = list()
for i in range(100_000):
l.append(str(i))
start = now()
s = ",".join(l)
end = now()
print('Len: ', len(s), 'Time: ', (end - start), 'seconds')

if __name__ == '__main__':
main()
154 changes: 154 additions & 0 deletions big_text.txt

Large diffs are not rendered by default.

753 changes: 753 additions & 0 deletions c-abi.txt

Large diffs are not rendered by default.

196 changes: 196 additions & 0 deletions dict_benchmark.mojo

Large diffs are not rendered by default.

29 changes: 29 additions & 0 deletions dict_benchmark2.mojo
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from collections import Dict
from time import now
from random import *

alias iteration_size = 2048
def main():
var result: Int=0
var start = now()
var stop = now()

small2 = Dict[Int,Int]()
start = now()
for x in range(100):
for i in range(iteration_size):
small2[i] = i
for i in range(iteration_size):
result += small2[i]
stop = now()
print("Int dicts:", stop-start, "ns", result, "rows")

small3 = Dict[String,String]()
start = now()
for x in range(100):
for i in range(iteration_size):
small3[str(i)]=str(i)
for i in range(iteration_size):
result += len(small3[str(i)])
stop = now()
print("String dicts:", stop-start, "ns", result, "rows")
4 changes: 4 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ what we publish.

### ⭐️ New

- New `Int.from_bytes()` and `Int.as_bytes()` functions to convert a list of
bytes to an integer and vice versa, accepting the endianess as an argument.
Similar to Python `int.from_bytes()` and `int.to_bytes()` functions.

- `StringRef` is now representable so `repr(StringRef("hello"))` will return
`StringRef('hello')`.

Expand Down
Loading
Loading