{"id":1420,"date":"2024-11-12T07:19:33","date_gmt":"2024-11-12T07:19:33","guid":{"rendered":"https:\/\/zahiralam.com\/blog\/?p=1420"},"modified":"2024-11-12T07:44:34","modified_gmt":"2024-11-12T07:44:34","slug":"master-omnigen-setup-on-mac-m1-m2-m3-your-easy-step-by-step-guide","status":"publish","type":"post","link":"https:\/\/zahiralam.com\/blog\/master-omnigen-setup-on-mac-m1-m2-m3-your-easy-step-by-step-guide\/","title":{"rendered":"Master OmniGen Setup on Mac M1, M2, M3: Your Easy Step-by-Step Guide"},"content":{"rendered":"\n<p>Setting up OmniGen on Apple Silicon Macs can be a bit tricky due to compatibility issues with the MPS (Metal Performance Shaders) backend. Here\u2019s a comprehensive guide to help you get OmniGen running smoothly on your Mac M1, M2, or M3.\n\n\n\n<p><strong>1. Create a Conda Environment<\/strong>\n\n\n\n<p>Start by creating and activating a new Conda environment to isolate your OmniGen installation:\n\n\n\n<div class=\"code-block-container\">\n                        <pre class=\"wp-block-code\"><code id=\"code-1\">conda create -n omnigen python=3.10\nconda activate omnigen<\/code><\/pre>\n                        <amp-iframe sandbox=\"allow-scripts\" width=\"94\" height=\"72\" frameborder=\"0\" \n                                    src=\"https:\/\/zahiralam.com\/blog\/wp-content\/plugins\/amp-copy-code-button\/copier.html#conda%20create%20-n%20omnigen%20python%3D3.10%0Aconda%20activate%20omnigen\">\n                            <button class=\"copy-button\" data-label=\"conda create -n omnigen python=3.10\nconda activate omnigen\"  placeholder disabled>Copy<\/button>\n                        <\/amp-iframe>\n                    <\/div>\n\n\n\n<p><strong>2. Install PyTorch with MPS Support<\/strong>\n\n\n\n<p>On Apple Silicon Macs, PyTorch uses MPS for GPU acceleration. Install PyTorch and the necessary libraries using <code>pip<\/code>:\n\n\n\n<div class=\"code-block-container\">\n                        <pre class=\"wp-block-code\"><code id=\"code-2\">pip install torch torchvision torchaudio<\/code><\/pre>\n                        <amp-iframe sandbox=\"allow-scripts\" width=\"94\" height=\"72\" frameborder=\"0\" \n                                    src=\"https:\/\/zahiralam.com\/blog\/wp-content\/plugins\/amp-copy-code-button\/copier.html#pip%20install%20torch%20torchvision%20torchaudio\">\n                            <button class=\"copy-button\" data-label=\"pip install torch torchvision torchaudio\"  placeholder disabled>Copy<\/button>\n                        <\/amp-iframe>\n                    <\/div>\n\n\n\n<p>To confirm that PyTorch is installed correctly and MPS is available, run:\n\n\n\n<div class=\"code-block-container\">\n                        <pre class=\"wp-block-code\"><code id=\"code-3\">python -c &quot;import torch; print(torch.__version__, torch.backends.mps.is_available())&quot;<\/code><\/pre>\n                        <amp-iframe sandbox=\"allow-scripts\" width=\"94\" height=\"72\" frameborder=\"0\" \n                                    src=\"https:\/\/zahiralam.com\/blog\/wp-content\/plugins\/amp-copy-code-button\/copier.html#python%20-c%20%22import%20torch%3B%20print%28torch.__version__%2C%20torch.backends.mps.is_available%28%29%29%22\">\n                            <button class=\"copy-button\" data-label=\"python -c &quot;import torch; print(torch.__version__, torch.backends.mps.is_available())&quot;\"  placeholder disabled>Copy<\/button>\n                        <\/amp-iframe>\n                    <\/div>\n\n\n\n<p>Make sure that the output confirms MPS is available (<code>True<\/code>).\n\n\n\n<p><strong>3. Clone and Install OmniGen<\/strong>\n\n\n\n<p>Clone the OmniGen repository and install it in editable mode:\n\n\n\n<div class=\"code-block-container\">\n                        <pre class=\"wp-block-code\"><code id=\"code-4\">git clone https:\/\/github.com\/staoxiao\/OmniGen.git\ncd OmniGen\npip install -e .<\/code><\/pre>\n                        <amp-iframe sandbox=\"allow-scripts\" width=\"94\" height=\"72\" frameborder=\"0\" \n                                    src=\"https:\/\/zahiralam.com\/blog\/wp-content\/plugins\/amp-copy-code-button\/copier.html#git%20clone%20https%3A%2F%2Fgithub.com%2Fstaoxiao%2FOmniGen.git%0Acd%20OmniGen%0Apip%20install%20-e%20.\">\n                            <button class=\"copy-button\" data-label=\"git clone https:\/\/github.com\/staoxiao\/OmniGen.git\ncd OmniGen\npip install -e .\"  placeholder disabled>Copy<\/button>\n                        <\/amp-iframe>\n                    <\/div>\n\n\n\n<p><strong>Check the Installation:<\/strong>\n\n\n\n<p>To ensure OmniGen is installed successfully, run:\n\n\n\n<div class=\"code-block-container\">\n                        <pre class=\"wp-block-code\"><code id=\"code-5\">python -c &quot;import OmniGen; print(&#039;OmniGen installed successfully&#039;)&quot;<\/code><\/pre>\n                        <amp-iframe sandbox=\"allow-scripts\" width=\"94\" height=\"72\" frameborder=\"0\" \n                                    src=\"https:\/\/zahiralam.com\/blog\/wp-content\/plugins\/amp-copy-code-button\/copier.html#python%20-c%20%22import%20OmniGen%3B%20print%28%27OmniGen%20installed%20successfully%27%29%22\">\n                            <button class=\"copy-button\" data-label=\"python -c &quot;import OmniGen; print(&#039;OmniGen installed successfully&#039;)&quot;\"  placeholder disabled>Copy<\/button>\n                        <\/amp-iframe>\n                    <\/div>\n\n\n\n<p><strong>4. Create and Modify the <code>omnigen_example.py<\/code> File<\/strong>\n\n\n\n<p>Create <code>omnigen_example.py<\/code> with the following content:\n\n\n\n<div class=\"code-block-container\">\n                        <pre class=\"wp-block-code\"><code id=\"code-6\">from OmniGen import OmniGenPipeline\nimport torch\n\n# Check if MPS (Metal Performance Shaders) is available\ndevice = torch.device(&quot;mps&quot;) if torch.backends.mps.is_available() else torch.device(&quot;cpu&quot;)\n\n# Load the OmniGen model\npipe = OmniGenPipeline.from_pretrained(&quot;Shitao\/OmniGen-v1&quot;)\n\n# Move the model to the specified device\npipe.to(device)\n\n# Generate an image from text\nimages = pipe(\n    prompt=&quot;A curly-haired man in a red shirt is drinking tea.&quot;,\n    height=512,  # Reduced size to manage memory usage\n    width=512,\n    guidance_scale=2.5,\n    seed=0,\n    use_kv_cache=False,  # Disable key-value cache\n    offload_kv_cache=False  # Disable offloading\n)\n\n# Save the output image\nimages[0].save(&quot;example_t2i.png&quot;)<\/code><\/pre>\n                        <amp-iframe sandbox=\"allow-scripts\" width=\"94\" height=\"72\" frameborder=\"0\" \n                                    src=\"https:\/\/zahiralam.com\/blog\/wp-content\/plugins\/amp-copy-code-button\/copier.html#from%20OmniGen%20import%20OmniGenPipeline%0Aimport%20torch%0A%0A%23%20Check%20if%20MPS%20%28Metal%20Performance%20Shaders%29%20is%20available%0Adevice%20%3D%20torch.device%28%22mps%22%29%20if%20torch.backends.mps.is_available%28%29%20else%20torch.device%28%22cpu%22%29%0A%0A%23%20Load%20the%20OmniGen%20model%0Apipe%20%3D%20OmniGenPipeline.from_pretrained%28%22Shitao%2FOmniGen-v1%22%29%0A%0A%23%20Move%20the%20model%20to%20the%20specified%20device%0Apipe.to%28device%29%0A%0A%23%20Generate%20an%20image%20from%20text%0Aimages%20%3D%20pipe%28%0A%20%20%20%20prompt%3D%22A%20curly-haired%20man%20in%20a%20red%20shirt%20is%20drinking%20tea.%22%2C%0A%20%20%20%20height%3D512%2C%20%20%23%20Reduced%20size%20to%20manage%20memory%20usage%0A%20%20%20%20width%3D512%2C%0A%20%20%20%20guidance_scale%3D2.5%2C%0A%20%20%20%20seed%3D0%2C%0A%20%20%20%20use_kv_cache%3DFalse%2C%20%20%23%20Disable%20key-value%20cache%0A%20%20%20%20offload_kv_cache%3DFalse%20%20%23%20Disable%20offloading%0A%29%0A%0A%23%20Save%20the%20output%20image%0Aimages%5B0%5D.save%28%22example_t2i.png%22%29\">\n                            <button class=\"copy-button\" data-label=\"from OmniGen import OmniGenPipeline\nimport torch\n\n# Check if MPS (Metal Performance Shaders) is available\ndevice = torch.device(&quot;mps&quot;) if torch.backends.mps.is_available() else torch.device(&quot;cpu&quot;)\n\n# Load the OmniGen model\npipe = OmniGenPipeline.from_pretrained(&quot;Shitao\/OmniGen-v1&quot;)\n\n# Move the model to the specified device\npipe.to(device)\n\n# Generate an image from text\nimages = pipe(\n    prompt=&quot;A curly-haired man in a red shirt is drinking tea.&quot;,\n    height=512,  # Reduced size to manage memory usage\n    width=512,\n    guidance_scale=2.5,\n    seed=0,\n    use_kv_cache=False,  # Disable key-value cache\n    offload_kv_cache=False  # Disable offloading\n)\n\n# Save the output image\nimages[0].save(&quot;example_t2i.png&quot;)\"  placeholder disabled>Copy<\/button>\n                        <\/amp-iframe>\n                    <\/div>\n\n\n\n<p>This script adjusts the device settings and reduces the image dimensions to prevent memory issues on Apple Silicon Macs.\n\n\n\n<p><strong>5. Run the <code>omnigen_example.py<\/code> Script<\/strong>\n\n\n\n<p>To generate an image using OmniGen, execute the script:\n\n\n\n<div class=\"code-block-container\">\n                        <pre class=\"wp-block-code\"><code id=\"code-7\">python omnigen_example.py<\/code><\/pre>\n                        <amp-iframe sandbox=\"allow-scripts\" width=\"94\" height=\"72\" frameborder=\"0\" \n                                    src=\"https:\/\/zahiralam.com\/blog\/wp-content\/plugins\/amp-copy-code-button\/copier.html#python%20omnigen_example.py\">\n                            <button class=\"copy-button\" data-label=\"python omnigen_example.py\"  placeholder disabled>Copy<\/button>\n                        <\/amp-iframe>\n                    <\/div>\n\n\n\n<p>This should create and save the image <code>example_t2i.png<\/code> without running into memory problems.\n\n\n\n<h3 class=\"wp-block-heading\">Additional Tips<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Memory Management<\/strong>: If you encounter memory-related errors, try reducing the image size further or setting environment variables to optimize memory allocation:<\/li>\n<\/ul>\n\n\n\n<div class=\"code-block-container\">\n                        <pre class=\"wp-block-code\"><code id=\"code-8\">export PYTORCH_MPS_HIGH_WATERMARK_RATIO=0.5<\/code><\/pre>\n                        <amp-iframe sandbox=\"allow-scripts\" width=\"94\" height=\"72\" frameborder=\"0\" \n                                    src=\"https:\/\/zahiralam.com\/blog\/wp-content\/plugins\/amp-copy-code-button\/copier.html#export%20PYTORCH_MPS_HIGH_WATERMARK_RATIO%3D0.5\">\n                            <button class=\"copy-button\" data-label=\"export PYTORCH_MPS_HIGH_WATERMARK_RATIO=0.5\"  placeholder disabled>Copy<\/button>\n                        <\/amp-iframe>\n                    <\/div>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Resource Monitoring<\/strong>: Monitor your system&#8217;s resource usage to ensure optimal performance.<\/li>\n<\/ul>\n\n\n\n<p>By following these steps, you\u2019ll have OmniGen up and running on your Mac M1, M2, or M3, leveraging the MPS backend for efficient processing. If you have any issues, refer back to the guide to troubleshoot and optimize your setup. Enjoy creating images with OmniGen on your Apple Silicon Mac!\n","protected":false},"excerpt":{"rendered":"<p>Setting up OmniGen on Apple Silicon Macs can be a bit tricky due to compatibility issues with the MPS (Metal Performance Shaders) backend. Here\u2019s a [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":1430,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[24],"tags":[17,253,229],"class_list":["post-1420","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-artificial-intelligence","tag-mac-m1-m2-m3","tag-omnigen","tag-text-to-image"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/zahiralam.com\/blog\/wp-json\/wp\/v2\/posts\/1420","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zahiralam.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zahiralam.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zahiralam.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zahiralam.com\/blog\/wp-json\/wp\/v2\/comments?post=1420"}],"version-history":[{"count":6,"href":"https:\/\/zahiralam.com\/blog\/wp-json\/wp\/v2\/posts\/1420\/revisions"}],"predecessor-version":[{"id":1427,"href":"https:\/\/zahiralam.com\/blog\/wp-json\/wp\/v2\/posts\/1420\/revisions\/1427"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/zahiralam.com\/blog\/wp-json\/wp\/v2\/media\/1430"}],"wp:attachment":[{"href":"https:\/\/zahiralam.com\/blog\/wp-json\/wp\/v2\/media?parent=1420"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zahiralam.com\/blog\/wp-json\/wp\/v2\/categories?post=1420"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zahiralam.com\/blog\/wp-json\/wp\/v2\/tags?post=1420"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}