Files
mlx/docs/build/doctrees/usage/quick_start.doctree

74 lines
10 KiB
Plaintext
Raw Normal View History

2024-01-17 17:15:29 -08:00
<EFBFBD><05>t)<00>sphinx.addnodes<65><73>document<6E><74><EFBFBD>)<29><>}<7D>(<28> rawsource<63><65><00><>children<65>]<5D><>docutils.nodes<65><73>section<6F><6E><EFBFBD>)<29><>}<7D>(hhh]<5D>(h <09>title<6C><65><EFBFBD>)<29><>}<7D>(h<05>Quick Start Guide<64>h]<5D>h <09>Text<78><74><EFBFBD><EFBFBD>Quick Start Guide<64><65><EFBFBD><EFBFBD><EFBFBD>}<7D>(<28>parent<6E>h<11> _document<6E>h<03>source<63>N<EFBFBD>line<6E>Nuba<62>
attributes<EFBFBD>}<7D>(<28>ids<64>]<5D><>classes<65>]<5D><>names<65>]<5D><>dupnames<65>]<5D><>backrefs<66>]<5D>u<EFBFBD>tagname<6D>hhh hhh<1D>:/Users/awnihannun/repos/mlx/docs/src/usage/quick_start.rst<73>hKubh )<29><>}<7D>(hhh]<5D>(h)<29><>}<7D>(h<05>Basics<63>h]<5D>h<16>Basics<63><73><EFBFBD><EFBFBD><EFBFBD>}<7D>(hh0hhhNhNubah}<7D>(h!]<5D>h#]<5D>h%]<5D>h']<5D>h)]<5D>uh+hhh-hhhh,hKubh <09> paragraph<70><68><EFBFBD>)<29><>}<7D>(h<05>/Import ``mlx.core`` and make an :class:`array`:<3A>h]<5D>(h<16>Import <20><><EFBFBD><EFBFBD><EFBFBD>}<7D>(hh@hhhNhNubh <09>literal<61><6C><EFBFBD>)<29><>}<7D>(h<05> ``mlx.core``<60>h]<5D>h<16>mlx.core<72><65><EFBFBD><EFBFBD><EFBFBD>}<7D>(hhJhhhNhNubah}<7D>(h!]<5D>h#]<5D>h%]<5D>h']<5D>h)]<5D>uh+hHhh@ubh<16> and make an <20><><EFBFBD><EFBFBD><EFBFBD>}<7D>(hh@hhhNhNubh<00> pending_xref<65><66><EFBFBD>)<29><>}<7D>(h<05>:class:`array`<60>h]<5D>hI)<29><>}<7D>(hh`h]<5D>h<16>array<61><79><EFBFBD><EFBFBD><EFBFBD>}<7D>(hhbhhhNhNubah}<7D>(h!]<5D>h#]<5D>(<28>xref<65><66>py<70><79>py-class<73>eh%]<5D>h']<5D>h)]<5D>uh+hHhh^ubah}<7D>(h!]<5D>h#]<5D>h%]<5D>h']<5D>h)]<5D><>refdoc<6F><63>usage/quick_start<72><74> refdomain<69>hm<68>reftype<70><65>class<73><73> refexplicit<69><74><EFBFBD>refwarn<72><6E><EFBFBD> py:module<6C><65>mlx.core<72><65>py:class<73>N<EFBFBD> reftarget<65><74>array<61>uh+h\hh,hK
hh@ubh<16>:<3A><><EFBFBD><EFBFBD><EFBFBD>}<7D>(hh@hhhNhNubeh}<7D>(h!]<5D>h#]<5D>h%]<5D>h']<5D>h)]<5D>uh+h>hh,hK
hh-hhubh <09> literal_block<63><6B><EFBFBD>)<29><>}<7D>(h<05><>>> import mlx.core as mx
>> a = mx.array([1, 2, 3, 4])
>> a.shape
[4]
>> a.dtype
int32
>> b = mx.array([1.0, 2.0, 3.0, 4.0])
>> b.dtype
float32<EFBFBD>h]<5D>h<16><>>> import mlx.core as mx
>> a = mx.array([1, 2, 3, 4])
>> a.shape
[4]
>> a.dtype
int32
>> b = mx.array([1.0, 2.0, 3.0, 4.0])
>> b.dtype
float32<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>}<7D>hh<>sbah}<7D>(h!]<5D>h#]<5D>h%]<5D>h']<5D>h)]<5D><> xml:space<63><65>preserve<76><65>force<63><65><EFBFBD>language<67><65>python<6F><6E>highlight_args<67>}<7D>uh+h<>hh,hK hh-hhubh?)<29><>}<7D>(hX<>Operations in MLX are lazy. The outputs of MLX operations are not computed
until they are needed. To force an array to be evaluated use
:func:`eval`. Arrays will automatically be evaluated in a few cases. For
example, inspecting a scalar with :meth:`array.item`, printing an array,
or converting an array from :class:`array` to :class:`numpy.ndarray` all
automatically evaluate the array.<2E>h]<5D>(h<16><>Operations in MLX are lazy. The outputs of MLX operations are not computed
until they are needed. To force an array to be evaluated use
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>}<7D>(hh<>hhhNhNubh])<29><>}<7D>(h<05> :func:`eval`<60>h]<5D>hI)<29><>}<7D>(hh<>h]<5D>h<16>eval()<29><><EFBFBD><EFBFBD><EFBFBD>}<7D>(hh<>hhhNhNubah}<7D>(h!]<5D>h#]<5D>(hl<68>py<70><79>py-func<6E>eh%]<5D>h']<5D>h)]<5D>uh+hHhh<>ubah}<7D>(h!]<5D>h#]<5D>h%]<5D>h']<5D>h)]<5D><>refdoc<6F>hy<68> refdomain<69>h<EFBFBD><68>reftype<70><65>func<6E><63> refexplicit<69><74><EFBFBD>refwarn<72><6E>hh<7F>h<EFBFBD>Nh<4E><68>eval<61>uh+h\hh,hKhh<>ubh<16>`. Arrays will automatically be evaluated in a few cases. For
example, inspecting a scalar with <20><><EFBFBD><EFBFBD><EFBFBD>}<7D>(hh<>hhhNhNubh])<29><>}<7D>(h<05>:meth:`array.item`<60>h]<5D>hI)<29><>}<7D>(hh<>h]<5D>h<16> array.item()<29><><EFBFBD><EFBFBD><EFBFBD>}<7D>(hh<>hhhNhNubah}<7D>(h!]<5D>h#]<5D>(hl<68>py<70><79>py-meth<74>eh%]<5D>h']<5D>h)]<5D>uh+hHhh<>ubah}<7D>(h!]<5D>h#]<5D>h%]<5D>h']<5D>h)]<5D><>refdoc<6F>hy<68> refdomain<69>reftype<70><65>meth<74><68> refexplicit<69><74><EFBFBD>refwarn<72><6E>hh<7F>h<EFBFBD>Nh<4E><68>
array.item<65>uh+h\hh,hKhh<>ubh<16>1, printing an array,
or converting an array from <20><><EFBFBD><EFBFBD><EFBFBD>}<7D>(hh<>hhhNhNubh])<29><>}<7D>(h<05>:class:`array`<60>h]<5D>hI)<29><>}<7D>(hh<>h]<5D>h<16>array<61><79><EFBFBD><EFBFBD><EFBFBD>}<7D>(hh<>hhhNhNubah}<7D>(h!]<5D>h#]<5D>(hl<68>py<70><79>py-class<73>eh%]<5D>h']<5D>h)]<5D>uh+hHhh<>ubah}<7D>(h!]<5D>h#]<5D>h%]<5D>h']<5D>h)]<5D><>refdoc<6F>hy<68> refdomain<69>j<00>reftype<70><65>class<73><73> refexplicit<69><74><EFBFBD>refwarn<72><6E>hh<7F>h<EFBFBD>Nh<4E><68>array<61>uh+h\hh,hKhh<>ubh<16> to <20><><EFBFBD><EFBFBD><EFBFBD>}<7D>(hh<>hhhNhNubh])<29><>}<7D>(h<05>:class:`numpy.ndarray`<60>h]<5D>hI)<29><>}<7D>(hjh]<5D>h<16> numpy.ndarray<61><79><EFBFBD><EFBFBD><EFBFBD>}<7D>(hjhhhNhNubah}<7D>(h!]<5D>h#]<5D>(hl<68>py<70><79>py-class<73>eh%]<5D>h']<5D>h)]<5D>uh+hHhjubah}<7D>(h!]<5D>h#]<5D>h%]<5D>h']<5D>h)]<5D><>refdoc<6F>hy<68> refdomain<69>j'<00>reftype<70><65>class<73><73> refexplicit<69><74><EFBFBD>refwarn<72><6E>hh<7F>h<EFBFBD>Nh<4E><68> numpy.ndarray<61>uh+h\hh,hKhh<>ubh<16>& all
automatically evaluate the array.<2E><><EFBFBD><EFBFBD><EFBFBD>}<7D>(hh<>hhhNhNubeh}<7D>(h!]<5D>h#]<5D>h%]<5D>h']<5D>h)]<5D>uh+h>hh,hKhh-hhubh<62>)<29><>}<7D>(hX>> c = a + b # c not yet evaluated
>> mx.eval(c) # evaluates c
>> c = a + b
>> print(c) # Also evaluates c
array([2, 4, 6, 8], dtype=float32)
>> c = a + b
>> import numpy as np
>> np.array(c) # Also evaluates c
array([2., 4., 6., 8.], dtype=float32)<29>h]<5D>hX>> c = a + b # c not yet evaluated
>> mx.eval(c) # evaluates c
>> c = a + b
>> print(c) # Also evaluates c
array([2, 4, 6, 8], dtype=float32)
>> c = a + b
>> import numpy as np
>> np.array(c) # Also evaluates c
array([2., 4., 6., 8.], dtype=float32)<29><><EFBFBD><EFBFBD><EFBFBD>}<7D>hjCsbah}<7D>(h!]<5D>h#]<5D>h%]<5D>h']<5D>h)]<5D>h<EFBFBD>h<EFBFBD>h<EFBFBD><68>h<EFBFBD><68>python<6F>h<EFBFBD>}<7D>uh+h<>hh,hKhh-hhubh?)<29><>}<7D>(h<05>DSee the page on :ref:`Lazy Evaluation <lazy eval>` for more details.<2E>h]<5D>(h<16>See the page on <20><><EFBFBD><EFBFBD><EFBFBD>}<7D>(hjShhhNhNubh])<29><>}<7D>(h<05>":ref:`Lazy Evaluation <lazy eval>`<60>h]<5D>h <09>inline<6E><65><EFBFBD>)<29><>}<7D>(hj]h]<5D>h<16>Lazy Evaluation<6F><6E><EFBFBD><EFBFBD><EFBFBD>}<7D>(hjahhhNhNubah}<7D>(h!]<5D>h#]<5D>(hl<68>std<74><64>std-ref<65>eh%]<5D>h']<5D>h)]<5D>uh+j_hj[ubah}<7D>(h!]<5D>h#]<5D>h%]<5D>h']<5D>h)]<5D><>refdoc<6F>hy<68> refdomain<69>jk<00>reftype<70><65>ref<65><66> refexplicit<69><74><EFBFBD>refwarn<72><6E>h<EFBFBD><68> lazy eval<61>uh+h\hh,hK,hjSubh<16> for more details.<2E><><EFBFBD><EFBFBD><EFBFBD>}<7D>(hjShhhNhNubeh}<7D>(h!]<5D>h#]<5D>h%]<5D>h']<5D>h)]<5D>uh+h>hh,hK,hh-hhubeh}<7D>(h!]<5D><>basics<63>ah#]<5D>h%]<5D><>basics<63>ah']<5D>h)]<5D>uh+h
hh hhhh,hKubh )<29><>}<7D>(hhh]<5D>(h)<29><>}<7D>(h<05>"Function and Graph Transformations<6E>h]<5D>h<16>"Function and Graph Transformations<6E><73><EFBFBD><EFBFBD><EFBFBD>}<7D>(hj<>hhhNhNubah}<7D>(h!]<5D>h#]<5D>h%]<5D>h']<5D>h)]<5D>uh+hhj<>hhhh,hK/ubh?)<29><>}<7D>(h<05><>MLX has standard function transformations like :func:`grad` and :func:`vmap`.
Transformations can be composed arbitrarily. For example
``grad(vmap(grad(fn)))`` (or any other composition) is allowed.<2E>h]<5D>(h<16>/MLX has standard function transformations like <20><><EFBFBD><EFBFBD><EFBFBD>}<7D>(hj<>hhhNhNubh])<29><>}<7D>(h<05> :func:`grad`<60>h]<5D>hI)<29><>}<7D>(hj<>h]<5D>h<16>grad()<29><><EFBFBD><EFBFBD><EFBFBD>}<7D>(hj<>hhhNhNubah}<7D>(h!]<5D>h#]<5D>(hl<68>py<70><79>py-func<6E>eh%]<5D>h']<5D>h)]<5D>uh+hHhj<>ubah}<7D>(h!]<5D>h#]<5D>h%]<5D>h']<5D>h)]<5D><>refdoc<6F>hy<68> refdomain<69>j<EFBFBD><00>reftype<70><65>func<6E><63> refexplicit<69><74><EFBFBD>refwarn<72><6E>hh<7F>h<EFBFBD>Nh<4E><68>grad<61>uh+h\hh,hK1hj<>ubh<16> and <20><><EFBFBD><EFBFBD><EFBFBD>}<7D>(hj<>hhhNhNubh])<29><>}<7D>(h<05> :func:`vmap`<60>h]<5D>hI)<29><>}<7D>(hj<>h]<5D>h<16>vmap()<29><><EFBFBD><EFBFBD><EFBFBD>}<7D>(hj<>hhhNhNubah}<7D>(h!]<5D>h#]<5D>(hl<68>py<70><79>py-func<6E>eh%]<5D>h']<5D>h)]<5D>uh+hHhj<>ubah}<7D>(h!]<5D>h#]<5D>h%]<5D>h']<5D>h)]<5D><>refdoc<6F>hy<68> refdomain<69>j<EFBFBD><00>reftype<70><65>func<6E><63> refexplicit<69><74><EFBFBD>refwarn<72><6E>hh<7F>h<EFBFBD>Nh<4E><68>vmap<61>uh+h\hh,hK1hj<>ubh<16>;.
Transformations can be composed arbitrarily. For example
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>}<7D>(hj<>hhhNhNubhI)<29><>}<7D>(h<05>``grad(vmap(grad(fn)))``<60>h]<5D>h<16>grad(vmap(grad(fn)))<29><><EFBFBD><EFBFBD><EFBFBD>}<7D>(hj<>hhhNhNubah}<7D>(h!]<5D>h#]<5D>h%]<5D>h']<5D>h)]<5D>uh+hHhj<>ubh<16>' (or any other composition) is allowed.<2E><><EFBFBD><EFBFBD><EFBFBD>}<7D>(hj<>hhhNhNubeh}<7D>(h!]<5D>h#]<5D>h%]<5D>h']<5D>h)]<5D>uh+h>hh,hK1hj<>hhubh<62>)<29><>}<7D>(h<05><>>> x = mx.array(0.0)
>> mx.sin(x)
array(0, dtype=float32)
>> mx.grad(mx.sin)(x)
array(1, dtype=float32)
>> mx.grad(mx.grad(mx.sin))(x)
array(-0, dtype=float32)<29>h]<5D>h<16><>>> x = mx.array(0.0)
>> mx.sin(x)
array(0, dtype=float32)
>> mx.grad(mx.sin)(x)
array(1, dtype=float32)
>> mx.grad(mx.grad(mx.sin))(x)
array(-0, dtype=float32)<29><><EFBFBD><EFBFBD><EFBFBD>}<7D>hjsbah}<7D>(h!]<5D>h#]<5D>h%]<5D>h']<5D>h)]<5D>h<EFBFBD>h<EFBFBD>h<EFBFBD><68>h<EFBFBD><68>python<6F>h<EFBFBD>}<7D>uh+h<>hh,hK5hj<>hhubh?)<29><>}<7D>(h<05>}Other gradient transformations include :func:`vjp` for vector-Jacobian products
and :func:`jvp` for Jacobian-vector products.<2E>h]<5D>(h<16>'Other gradient transformations include <20><><EFBFBD><EFBFBD><EFBFBD>}<7D>(hjhhhNhNubh])<29><>}<7D>(h<05> :func:`vjp`<60>h]<5D>hI)<29><>}<7D>(hj"h]<5D>h<16>vjp()<29><><EFBFBD><EFBFBD><EFBFBD>}<7D>(hj$hhhNhNubah}<7D>(h!]<5D>h#]<5D>(hl<68>py<70><79>py-func<6E>eh%]<5D>h']<5D>h)]<5D>uh+hHhj ubah}<7D>(h!]<5D>h#]<5D>h%]<5D>h']<5D>h)]<5D><>refdoc<6F>hy<68> refdomain<69>j.<00>reftype<70><65>func<6E><63> refexplicit<69><74><EFBFBD>refwarn<72><6E>hh<7F>h<EFBFBD>Nh<4E><68>vjp<6A>uh+h\hh,hK?hjubh<16>" for vector-Jacobian products
and <20><><EFBFBD><EFBFBD><EFBFBD>}<7D>(hjhhhNhNubh])<29><>}<7D>(h<05> :func:`jvp`<60>h]<5D>hI)<29><>}<7D>(hjFh]<5D>h<16>jvp()<29><><EFBFBD><EFBFBD><EFBFBD>}<7D>(hjHhhhNhNubah}<7D>(h!]<5D>h#]<5D>(hl<68>py<70><79>py-func<6E>eh%]<5D>h']<5D>h)]<5D>uh+hHhjDubah}<7D>(h!]<5D>h#]<5D>h%]<5D>h']<5D>h)]<5D><>refdoc<6F>hy<68> refdomain<69>jR<00>reftype<70><65>func<6E><63> refexplicit<69><74><EFBFBD>refwarn<72><6E>hh<7F>h<EFBFBD>Nh<4E><68>jvp<76>uh+h\hh,hK?hjubh<16> for Jacobian-vector products.<2E><><EFBFBD><EFBFBD><EFBFBD>}<7D>(hjhhhNhNubeh}<7D>(h!]<5D>h#]<5D>h%]<5D>h']<5D>h)]<5D>uh+h>hh,hK?hj<>hhubh?)<29><>}<7D>(h<05>}Use :func:`value_and_grad` to efficiently compute both a function's output and
gradient with respect to the function's input.<2E>h]<5D>(h<16>Use <20><><EFBFBD><EFBFBD><EFBFBD>}<7D>(hjnhhhNhNubh])<29><>}<7D>(h<05>:func:`value_and_grad`<60>h]<5D>hI)<29><>}<7D>(hjxh]<5D>h<16>value_and_grad()<29><><EFBFBD><EFBFBD><EFBFBD>}<7D>(hjzhhhNhNubah}<7D>(h!]<5D>h#]<5D>(hl<68>py<70><79>py-func<6E>eh%]<5D>h']<5D>h)]<5D>uh+hHhjvubah}<7D>(h!]<5D>h#]<5D>h%]<5D>h']<5D>h)]<5D><>refdoc<6F>hy<68> refdomain<69>j<EFBFBD><00>reftype<70><65>func<6E><63> refexplicit<69><74><EFBFBD>refwarn<72><6E>hh<7F>h<EFBFBD>Nh<4E><68>value_and_grad<61>uh+h\hh,hKBhjnubh<16>g to efficiently compute both a functions output and
gradient with respect to the functions input.<2E><><EFBFBD><EFBFBD><EFBFBD>}<7D>(hjnhhhNhNubeh}<7D>(h!]<5D>h#]<5D>h%]<5D>h']<5D>h)]<5D>uh+h>hh,hKBhj<>hhubeh}<7D>(h!]<5D><>"function-and-graph-transformations<6E>ah#]<5D>h%]<5D><>"function and graph transformations<6E>ah']<5D>h)]<5D>uh+h
hh hhhh,hK/ubeh}<7D>(h!]<5D><>quick-start-guide<64>ah#]<5D>h%]<5D><>quick start guide<64>ah']<5D>h)]<5D>uh+h
hhhhhh,hKubah}<7D>(h!]<5D>h#]<5D>h%]<5D>h']<5D>h)]<5D><>source<63>h,uh+h<01>current_source<63>N<EFBFBD> current_line<6E>N<EFBFBD>settings<67><73>docutils.frontend<6E><64>Values<65><73><EFBFBD>)<29><>}<7D>(hN<> generator<6F>N<EFBFBD> datestamp<6D>N<EFBFBD> source_link<6E>N<EFBFBD>
source_url<EFBFBD>N<EFBFBD> toc_backlinks<6B><73>entry<72><79>footnote_backlinks<6B>K<01> sectnum_xform<72>K<01>strip_comments<74>N<EFBFBD>strip_elements_with_classes<65>N<EFBFBD> strip_classes<65>N<EFBFBD> report_level<65>K<02>
halt_level<EFBFBD>K<05>exit_status_level<65>K<05>debug<75>N<EFBFBD>warning_stream<61>N<EFBFBD> traceback<63><6B><EFBFBD>input_encoding<6E><67> utf-8-sig<69><67>input_encoding_error_handler<65><72>strict<63><74>output_encoding<6E><67>utf-8<><38>output_encoding_error_handler<65>j<EFBFBD><00>error_encoding<6E><67>utf-8<><38>error_encoding_error_handler<65><72>backslashreplace<63><65> language_code<64><65>en<65><6E>record_dependencies<65>N<EFBFBD>config<69>N<EFBFBD> id_prefix<69>h<06>auto_id_prefix<69><78>id<69><64> dump_settings<67>N<EFBFBD>dump_internals<6C>N<EFBFBD>dump_transforms<6D>N<EFBFBD>dump_pseudo_xml<6D>N<EFBFBD>expose_internals<6C>N<EFBFBD>strict_visitor<6F>N<EFBFBD>_disable_config<69>N<EFBFBD>_source<63>h,<2C> _destination<6F>N<EFBFBD> _config_files<65>]<5D><>file_insertion_enabled<65><64><EFBFBD> raw_enabled<65>K<01>line_length_limit<69>M'<27>pep_references<65>N<EFBFBD> pep_base_url<72><6C>https://peps.python.org/<2F><>pep_file_url_template<74><65>pep-%04d<34><64>rfc_references<65>N<EFBFBD> rfc_base_url<72><6C>&https://datatracker.ietf.org/doc/html/<2F><> tab_width<74>K<08>trim_footnote_reference_space<63><65><EFBFBD>syntax_highlight<68><74>long<6E><67> smart_quotes<65><73><EFBFBD>smartquotes_locales<65>]<5D><>character_level_inline_markup<75><70><EFBFBD>doctitle_xform<72><6D><EFBFBD> docinfo_xform<72>K<01>sectsubtitle_xform<72><6D><EFBFBD> image_loading<6E><67>link<6E><6B>embed_stylesheet<65><74><EFBFBD>cloak_email_addresses<65><73><EFBFBD>section_self_link<6E><6B><EFBFBD>env<6E>Nub<75>reporter<65>N<EFBFBD>indirect_targets<74>]<5D><>substitution_defs<66>}<7D><>substitution_names<65>}<7D><>refnames<65>}<7D><>refids<64>}<7D><>nameids<64>}<7D>(j<>j<>j<>j<>j<>j<>u<> nametypes<65>}<7D>(j<><00>j<EFBFBD><00>j<EFBFBD><00>uh!}<7D>(j<>h j<>h-j<>j<>u<> footnote_refs<66>}<7D><> citation_refs<66>}<7D><> autofootnotes<65>]<5D><>autofootnote_refs<66>]<5D><>symbol_footnotes<65>]<5D><>symbol_footnote_refs<66>]<5D><> footnotes<65>]<5D><> citations<6E>]<5D><>autofootnote_start<72>K<01>symbol_footnote_start<72>K<00>
id_counter<EFBFBD><EFBFBD> collections<6E><73>Counter<65><72><EFBFBD>}<7D><><EFBFBD>R<EFBFBD><52>parse_messages<65>]<5D><>transform_messages<65>]<5D><> transformer<65>N<EFBFBD> include_log<6F>]<5D><>
decoration<EFBFBD>Nhhub.