Skip to content

Commit fb4b862

Browse files
authored
Add new equinox example (#4)
* new equinox * format
1 parent 2ead61d commit fb4b862

3 files changed

Lines changed: 41 additions & 23 deletions

File tree

examples/02_combining_metaclasses.ipynb

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,58 @@
22
"cells": [
33
{
44
"cell_type": "code",
5-
"execution_count": 1,
5+
"execution_count": 2,
66
"metadata": {},
7-
"outputs": [],
7+
"outputs": [
8+
{
9+
"name": "stdout",
10+
"output_type": "stream",
11+
"text": [
12+
"value: 1.0\n"
13+
]
14+
},
15+
{
16+
"data": {
17+
"text/plain": [
18+
"Array(2., dtype=float32, weak_type=True)"
19+
]
20+
},
21+
"execution_count": 2,
22+
"metadata": {},
23+
"output_type": "execute_result"
24+
}
25+
],
826
"source": [
9-
"# import equinox as eqx\n",
10-
"# import ihoop\n",
11-
"# import jax\n",
27+
"import equinox as eqx\n",
28+
"import ihoop\n",
29+
"import jax\n",
30+
"\n",
1231
"\n",
13-
"# # works with https://github.com/patrick-kidger/equinox/pull/1028\n",
14-
"# class _StrictEqxMeta(ihoop.strict._StrictMeta, eqx._module._module._ModuleMeta):\n",
15-
"# def __new__(mcs, name, bases, namespace, **kwargs):\n",
16-
"# return super().__new__(mcs, name, bases, namespace, **kwargs)\n",
32+
"class _StrictEqxMeta(ihoop.strict._StrictMeta, eqx._module._module._ModuleMeta):\n",
33+
" def __new__(mcs, name, bases, namespace, **kwargs):\n",
34+
" return super().__new__(mcs, name, bases, namespace, **kwargs)\n",
1735
"\n",
1836
"\n",
19-
"# class StrictModule(eqx.Module, ihoop.Strict, metaclass=_StrictEqxMeta):\n",
20-
"# pass\n",
37+
"class StrictModule(eqx.Module, ihoop.Strict, metaclass=_StrictEqxMeta):\n",
38+
" pass\n",
2139
"\n",
2240
"\n",
23-
"# class Test(StrictModule):\n",
24-
"# x: float\n",
41+
"class Test(StrictModule):\n",
42+
" x: float\n",
2543
"\n",
26-
"# def __init__(self):\n",
27-
"# self.x = 1.0\n",
44+
" def __init__(self):\n",
45+
" self.x = 1.0\n",
2846
"\n",
2947
"\n",
30-
"# t = Test()\n",
31-
"# print(\"value:\", t.x)\n",
48+
"t = Test()\n",
49+
"print(\"value:\", t.x)\n",
3250
"\n",
3351
"\n",
34-
"# def f(x):\n",
35-
"# return x.x**2\n",
52+
"def f(x):\n",
53+
" return x.x**2\n",
3654
"\n",
3755
"\n",
38-
"# jax.grad(f)(t).x"
56+
"jax.jit(jax.grad(f))(t).x"
3957
]
4058
},
4159
{

ihoop/strict.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ def __new__(
3737
"""
3838
Runs when a class inheriting from Strict is defined.
3939
"""
40-
# just check the initial letter as keyword, that way we can have multiple
40+
# just check the initial letters as keyword, that way we can have multiple
4141
# Strict classes that could resolve metaclass conflicts
4242
is_defining_strict_itself = (
43-
name == "Strict" and namespace.get("__module__") == mcs.__module__
43+
name[:6] == "Strict" and namespace.get("__module__") == mcs.__module__
4444
)
4545

4646
if not is_defining_strict_itself:

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ build-backend = "setuptools.build_meta"
2424

2525
[project.optional-dependencies]
2626
examples = [
27-
"equinox>=0.12.0"
27+
"equinox>=0.13.0"
2828
]
2929
testing = [
3030
"pytest==7.2.0",

0 commit comments

Comments
 (0)