Skip to content

Instantly share code, notes, and snippets.

View justinchuby's full-sized avatar
🌊
Better ML

Justin Chu justinchuby

🌊
Better ML
View GitHub Profile
@justinchuby
justinchuby / solve_Ax_eq_zero.py
Last active May 5, 2020 15:10
Use SVD to solve Ax=0 #linearalgebra #numpy
def solve_Ax_eq_zero(A):
_, _, vh = np.linalg.svd(A)
x = vh.T[:, -1]
return x
@justinchuby
justinchuby / python_typing.md
Last active May 15, 2022 16:05
Python container typing hierarchy

Python container typing hierarchy

Including collections abstract base classes

classDiagram
    AbstractSet <-- FrozenSet
    AbstractSet <-- KeysView
    AbstractSet <-- MutableSet
 MutableSet &lt;-- Set
@justinchuby
justinchuby / mypy_error_codes.py
Created January 6, 2023 03:38
Disable multiple mypy error codes for a file
# mypy: disable-error-code="misc,arg-type,type-arg,valid-type,assignment,return-value"
@justinchuby
justinchuby / gist:dbe99ce7ddbebb92d5d453f34afd3fa5
Created March 15, 2023 15:51
Python passes in self to its methods as the first argument
In [3]: def foo(*a): print(a)
In [4]: class Test:
...: func = foo
...: def bar(self):
...: self.func(42)
...:
In [5]: Test().bar()
(<__main__.Test object at 0x106751ab0>, 42)
@justinchuby
justinchuby / import_in_python.md
Last active March 21, 2024 18:46
Best practice for importing in Python

Best practice for importing in Python

1. Import at top level only

Allow imports at the module toplevel only, unless (1) it is too expensive to load the module or (2) module may not be available.

  • It is clear what a module needs when all imports are grouped in a single place. This makes refactoring easy.
  • Otherwise, any import errors will be raised only when the code executes. Erroneous imports may go undetected until the code path is hit at runtime.
  • Doing so reduces code duplication and improves consistency when we don't have the same import lines spread across the file.
  • Pylint has rule that checks for this: https://pylint.readthedocs.io/en/latest/user_guide/messages/convention/import-outside-toplevel.html
@justinchuby
justinchuby / retry.sh
Created August 30, 2023 16:11
Bash retry
# https://github.com/pytorch/pytorch/pull/106984/files
retry () {
"$@" || (sleep 10 && "$@") || (sleep 20 && "$@") || (sleep 40 && "$@")
}
@justinchuby
justinchuby / rotary_embedding.py
Created September 20, 2023 19:18
Rotary embedding ONNX export
import torch
from torch import nn
import onnx
class LlamaMSRotaryEmbedding(nn.Module):
def __init__(self, hidden_size, num_heads, max_sequence_length):
super().__init__()
self.hidden_size = hidden_size
@justinchuby
justinchuby / export_pytorch_onnx.py
Last active January 16, 2024 17:57
Test PyTorch ONNX export
import torch
import onnx
class Model(torch.nn.Module):
def forward(self, x):
return torch.nn.functional.hardsigmoid(x)
exported = torch.onnx.dynamo_export(Model(), torch.randn(1, 3, 224, 224))
print(onnx.printer.to_text(exported.model_proto))
@justinchuby
justinchuby / dynamo_text.py
Created March 13, 2024 00:55
Dynamo test
# onnxscript/tests/function_libs/torch_lib/dynamo_export_test.py
import copy
import inspect
import itertools
import sys
import unittest
import torch
from torch.onnx import ExportOptions
@justinchuby
justinchuby / requirements.txt
Created March 20, 2024 21:43
How to install ONNX Runtime Nightly
# https://aiinfra.visualstudio.com/PublicPackages/_artifacts/feed/ORT-Nightly/PyPI/ort-nightly/overview
--index-url=https://aiinfra.pkgs.visualstudio.com/PublicPackages/_packaging/ORT-Nightly/pypi/simple/
ort-nightly==1.17.0.dev20240118001