nexaai 1.0.19rc7__cp310-cp310-macosx_14_0_universal2.whl → 1.0.19rc8__cp310-cp310-macosx_14_0_universal2.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of nexaai might be problematic. Click here for more details.

Files changed (196) hide show
  1. nexaai/_stub.cpython-310-darwin.so +0 -0
  2. nexaai/_version.py +1 -1
  3. nexaai/binds/libnexa_bridge.dylib +0 -0
  4. {nexaai-1.0.19rc7.dist-info → nexaai-1.0.19rc8.dist-info}/METADATA +1 -1
  5. {nexaai-1.0.19rc7.dist-info → nexaai-1.0.19rc8.dist-info}/RECORD +7 -196
  6. nexaai/binds/nexa_mlx/py-lib/asr/__init__.py +0 -12
  7. nexaai/binds/nexa_mlx/py-lib/asr/interface.py +0 -122
  8. nexaai/binds/nexa_mlx/py-lib/common/__init__.py +0 -0
  9. nexaai/binds/nexa_mlx/py-lib/common/utils.py +0 -25
  10. nexaai/binds/nexa_mlx/py-lib/cv/__init__.py +0 -0
  11. nexaai/binds/nexa_mlx/py-lib/cv/generate.py +0 -195
  12. nexaai/binds/nexa_mlx/py-lib/cv/interface.py +0 -151
  13. nexaai/binds/nexa_mlx/py-lib/cv/main.py +0 -81
  14. nexaai/binds/nexa_mlx/py-lib/cv/modeling/pp_ocr_v4.py +0 -1736
  15. nexaai/binds/nexa_mlx/py-lib/embedding/__init__.py +0 -0
  16. nexaai/binds/nexa_mlx/py-lib/embedding/generate.py +0 -333
  17. nexaai/binds/nexa_mlx/py-lib/embedding/interface.py +0 -617
  18. nexaai/binds/nexa_mlx/py-lib/embedding/main.py +0 -173
  19. nexaai/binds/nexa_mlx/py-lib/embedding/modeling/__init__.py +0 -0
  20. nexaai/binds/nexa_mlx/py-lib/embedding/modeling/nexa_jina_v2.py +0 -399
  21. nexaai/binds/nexa_mlx/py-lib/image_gen/__init__.py +0 -1
  22. nexaai/binds/nexa_mlx/py-lib/image_gen/generate_sd.py +0 -244
  23. nexaai/binds/nexa_mlx/py-lib/image_gen/interface.py +0 -82
  24. nexaai/binds/nexa_mlx/py-lib/image_gen/main.py +0 -281
  25. nexaai/binds/nexa_mlx/py-lib/image_gen/stable_diffusion/__init__.py +0 -306
  26. nexaai/binds/nexa_mlx/py-lib/image_gen/stable_diffusion/clip.py +0 -116
  27. nexaai/binds/nexa_mlx/py-lib/image_gen/stable_diffusion/config.py +0 -65
  28. nexaai/binds/nexa_mlx/py-lib/image_gen/stable_diffusion/model_io.py +0 -386
  29. nexaai/binds/nexa_mlx/py-lib/image_gen/stable_diffusion/sampler.py +0 -105
  30. nexaai/binds/nexa_mlx/py-lib/image_gen/stable_diffusion/tokenizer.py +0 -100
  31. nexaai/binds/nexa_mlx/py-lib/image_gen/stable_diffusion/unet.py +0 -460
  32. nexaai/binds/nexa_mlx/py-lib/image_gen/stable_diffusion/vae.py +0 -274
  33. nexaai/binds/nexa_mlx/py-lib/llm/__init__.py +0 -0
  34. nexaai/binds/nexa_mlx/py-lib/llm/generate.py +0 -149
  35. nexaai/binds/nexa_mlx/py-lib/llm/interface.py +0 -764
  36. nexaai/binds/nexa_mlx/py-lib/llm/main.py +0 -68
  37. nexaai/binds/nexa_mlx/py-lib/rerank/__init__.py +0 -0
  38. nexaai/binds/nexa_mlx/py-lib/rerank/generate.py +0 -174
  39. nexaai/binds/nexa_mlx/py-lib/rerank/interface.py +0 -287
  40. nexaai/binds/nexa_mlx/py-lib/rerank/main.py +0 -127
  41. nexaai/binds/nexa_mlx/py-lib/rerank/modeling/__init__.py +0 -0
  42. nexaai/binds/nexa_mlx/py-lib/rerank/modeling/nexa_jina_rerank.py +0 -330
  43. nexaai/binds/nexa_mlx/py-lib/sd/__init__.py +0 -1
  44. nexaai/binds/nexa_mlx/py-lib/sd/interface.py +0 -362
  45. nexaai/binds/nexa_mlx/py-lib/sd/main.py +0 -286
  46. nexaai/binds/nexa_mlx/py-lib/sd/modeling/__init__.py +0 -306
  47. nexaai/binds/nexa_mlx/py-lib/sd/modeling/clip.py +0 -116
  48. nexaai/binds/nexa_mlx/py-lib/sd/modeling/config.py +0 -65
  49. nexaai/binds/nexa_mlx/py-lib/sd/modeling/model_io.py +0 -385
  50. nexaai/binds/nexa_mlx/py-lib/sd/modeling/sampler.py +0 -105
  51. nexaai/binds/nexa_mlx/py-lib/sd/modeling/tokenizer.py +0 -100
  52. nexaai/binds/nexa_mlx/py-lib/sd/modeling/unet.py +0 -460
  53. nexaai/binds/nexa_mlx/py-lib/sd/modeling/vae.py +0 -274
  54. nexaai/binds/nexa_mlx/py-lib/tts/__init__.py +0 -12
  55. nexaai/binds/nexa_mlx/py-lib/tts/interface.py +0 -276
  56. nexaai/binds/nexa_mlx/py-lib/vlm/__init__.py +0 -3
  57. nexaai/binds/nexa_mlx/py-lib/vlm/generate.py +0 -572
  58. nexaai/binds/nexa_mlx/py-lib/vlm/generate_qwen3_vl.py +0 -294
  59. nexaai/binds/nexa_mlx/py-lib/vlm/generate_qwen3_vl_moe.py +0 -276
  60. nexaai/binds/nexa_mlx/py-lib/vlm/interface.py +0 -504
  61. nexaai/binds/nexa_mlx/py-lib/vlm/main.py +0 -320
  62. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/__init__.py +0 -0
  63. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/convert.py +0 -68
  64. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/__init__.py +0 -0
  65. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/aya_vision/__init__.py +0 -8
  66. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/aya_vision/aya_vision.py +0 -193
  67. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/aya_vision/interpolate.py +0 -186
  68. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/aya_vision/language.py +0 -233
  69. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/aya_vision/vision.py +0 -503
  70. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/base.py +0 -202
  71. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/cache.py +0 -230
  72. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/deepseek_vl_v2/__init__.py +0 -10
  73. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/deepseek_vl_v2/conversation.py +0 -264
  74. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/deepseek_vl_v2/deepseek_vl_v2.py +0 -472
  75. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/deepseek_vl_v2/language.py +0 -591
  76. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/deepseek_vl_v2/processing_deepsek_vl_v2.py +0 -526
  77. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/deepseek_vl_v2/vision.py +0 -356
  78. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/florence2/__init__.py +0 -8
  79. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/florence2/florence2.py +0 -366
  80. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/florence2/language.py +0 -488
  81. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/florence2/vision.py +0 -591
  82. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/gemma3/__init__.py +0 -8
  83. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/gemma3/gemma3.py +0 -213
  84. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/gemma3/language.py +0 -315
  85. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/gemma3/vision.py +0 -238
  86. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/gemma3n/__init__.py +0 -2
  87. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/gemma3n/audio.py +0 -1038
  88. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/gemma3n/config.py +0 -139
  89. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/gemma3n/gemma3n.py +0 -322
  90. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/gemma3n/language.py +0 -629
  91. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/gemma3n/vision.py +0 -1022
  92. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/idefics2/__init__.py +0 -9
  93. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/idefics2/idefics2.py +0 -294
  94. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/idefics2/language.py +0 -191
  95. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/idefics2/vision.py +0 -267
  96. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/idefics3/__init__.py +0 -8
  97. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/idefics3/idefics3.py +0 -175
  98. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/idefics3/language.py +0 -192
  99. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/idefics3/vision.py +0 -233
  100. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/internvl_chat/__init__.py +0 -9
  101. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/internvl_chat/internvl_chat.py +0 -140
  102. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/internvl_chat/language.py +0 -220
  103. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/internvl_chat/processor.py +0 -393
  104. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/internvl_chat/vision.py +0 -293
  105. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/kernels.py +0 -307
  106. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/kimi_vl/__init__.py +0 -8
  107. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/kimi_vl/kimi_vl.py +0 -143
  108. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/kimi_vl/language.py +0 -509
  109. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/kimi_vl/vision.py +0 -522
  110. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/llama4/__init__.py +0 -8
  111. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/llama4/language.py +0 -386
  112. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/llama4/llama4.py +0 -138
  113. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/llama4/vision.py +0 -560
  114. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/llava/__init__.py +0 -8
  115. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/llava/language.py +0 -240
  116. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/llava/llava.py +0 -153
  117. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/llava/vision.py +0 -259
  118. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/llava_bunny/__init__.py +0 -9
  119. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/llava_bunny/language.py +0 -236
  120. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/llava_bunny/llava_bunny.py +0 -256
  121. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/llava_bunny/vision.py +0 -303
  122. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/llava_next/__init__.py +0 -8
  123. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/llava_next/language.py +0 -230
  124. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/llava_next/llava_next.py +0 -160
  125. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/llava_next/vision.py +0 -243
  126. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/mistral3/__init__.py +0 -8
  127. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/mistral3/mistral3.py +0 -283
  128. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/mllama/__init__.py +0 -8
  129. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/mllama/language.py +0 -416
  130. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/mllama/mllama.py +0 -172
  131. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/mllama/vision.py +0 -499
  132. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/molmo/__init__.py +0 -8
  133. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/molmo/language.py +0 -243
  134. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/molmo/molmo.py +0 -133
  135. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/molmo/vision.py +0 -465
  136. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/multi_modality/__init__.py +0 -10
  137. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/multi_modality/language.py +0 -230
  138. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/multi_modality/multi_modality.py +0 -385
  139. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/multi_modality/sam.py +0 -557
  140. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/multi_modality/vision.py +0 -526
  141. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/paligemma/__init__.py +0 -8
  142. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/paligemma/language.py +0 -282
  143. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/paligemma/paligemma.py +0 -160
  144. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/paligemma/vision.py +0 -242
  145. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/phi3_v/__init__.py +0 -8
  146. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/phi3_v/language.py +0 -21
  147. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/phi3_v/phi3_v.py +0 -243
  148. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/phi3_v/su_rope.py +0 -71
  149. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/phi3_v/vision.py +0 -324
  150. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/pixtral/__init__.py +0 -8
  151. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/pixtral/language.py +0 -229
  152. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/pixtral/pixtral.py +0 -161
  153. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/pixtral/vision.py +0 -320
  154. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen2_5_vl/__init__.py +0 -2
  155. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen2_5_vl/config.py +0 -108
  156. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen2_5_vl/language.py +0 -490
  157. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen2_5_vl/qwen2_5_vl.py +0 -168
  158. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen2_5_vl/vision.py +0 -414
  159. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen2_vl/__init__.py +0 -2
  160. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen2_vl/config.py +0 -104
  161. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen2_vl/language.py +0 -490
  162. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen2_vl/qwen2_vl.py +0 -167
  163. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen2_vl/vision.py +0 -312
  164. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3_vl/llm_common/__init__.py +0 -0
  165. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3_vl/llm_common/base.py +0 -117
  166. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3_vl/llm_common/cache.py +0 -531
  167. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3_vl/llm_common/generate.py +0 -701
  168. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3_vl/llm_common/rope_utils.py +0 -255
  169. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3_vl/llm_common/sample_utils.py +0 -303
  170. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3_vl/llm_common/tokenizer_utils.py +0 -407
  171. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3_vl/processor.py +0 -476
  172. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3_vl/qwen3vl.py +0 -1223
  173. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3vl_moe/llm_common/__init__.py +0 -0
  174. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3vl_moe/llm_common/base.py +0 -117
  175. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3vl_moe/llm_common/cache.py +0 -531
  176. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3vl_moe/llm_common/generate.py +0 -701
  177. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3vl_moe/llm_common/rope_utils.py +0 -255
  178. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3vl_moe/llm_common/sample_utils.py +0 -303
  179. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3vl_moe/llm_common/tokenizer_utils.py +0 -407
  180. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3vl_moe/processor.py +0 -476
  181. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3vl_moe/qwen3vl_moe.py +0 -1309
  182. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3vl_moe/switch_layers.py +0 -210
  183. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/smolvlm/__init__.py +0 -8
  184. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/smolvlm/smolvlm.py +0 -62
  185. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/processing_qwen2_5_vl.py +0 -209
  186. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/processing_qwen2_vl.py +0 -215
  187. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/prompt_utils.py +0 -474
  188. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/sample_utils.py +0 -39
  189. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/tokenizer_utils.py +0 -344
  190. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/trainer/__init__.py +0 -9
  191. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/trainer/lora.py +0 -70
  192. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/trainer/trainer.py +0 -296
  193. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/trainer/utils.py +0 -160
  194. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/utils.py +0 -928
  195. {nexaai-1.0.19rc7.dist-info → nexaai-1.0.19rc8.dist-info}/WHEEL +0 -0
  196. {nexaai-1.0.19rc7.dist-info → nexaai-1.0.19rc8.dist-info}/top_level.txt +0 -0
@@ -1,286 +0,0 @@
1
- from interface import ImageGen, ImageGenerationConfig, ImageSamplerConfig, Image
2
- import numpy as np
3
- from PIL import Image as PILImage
4
- import mlx.core as mx
5
-
6
-
7
- def test_txt2image(
8
- prompt="A photo of an astronaut riding a horse on Mars.",
9
- model="sdxl",
10
- local_model_path="",
11
- n_images=1,
12
- steps=None,
13
- cfg=None,
14
- negative_prompt="",
15
- n_rows=1,
16
- decoding_batch_size=1,
17
- float16=True,
18
- quantize=False,
19
- preload_models=False,
20
- output="out_txt2img.png",
21
- seed=None,
22
- verbose=False,
23
- width=512,
24
- height=512,
25
- ):
26
- """Generate images from text prompt using high-level interface"""
27
-
28
- # Determine model path based on model type
29
- if model == "sdxl":
30
- model_path = local_model_path or "stabilityai/sdxl-turbo"
31
- default_cfg = 0.0
32
- default_steps = 2
33
- else:
34
- model_path = local_model_path or "stabilityai/stable-diffusion-2-1-base"
35
- default_cfg = 7.5
36
- default_steps = 50
37
-
38
- # Use provided values or defaults
39
- cfg = cfg or default_cfg
40
- steps = steps or default_steps
41
-
42
- # Create ImageGen instance with proper parameters
43
- image_gen = ImageGen(model_path, "", device=None, float16=float16, quantize=quantize)
44
-
45
- # Load the model
46
- if not image_gen.load_model(model_path):
47
- print(f"Failed to load model: {model_path}")
48
- return None
49
-
50
- # Create sampler configuration
51
- sampler_config = ImageSamplerConfig(
52
- method="ddim",
53
- steps=steps,
54
- guidance_scale=cfg,
55
- seed=seed if seed is not None else -1,
56
- )
57
-
58
- # Create generation configuration with all parameters
59
- gen_config = ImageGenerationConfig(
60
- prompts=prompt,
61
- negative_prompts=negative_prompt,
62
- height=height,
63
- width=width,
64
- sampler_config=sampler_config,
65
- n_images=n_images,
66
- n_rows=n_rows,
67
- decoding_batch_size=decoding_batch_size,
68
- )
69
-
70
- if verbose:
71
- print(f"Generating {n_images} image(s) with prompt: '{prompt}'")
72
- print(f"Model: {model_path}, Steps: {steps}, CFG: {cfg}")
73
- print(f"Float16: {float16}, Quantize: {quantize}")
74
-
75
- # Generate image using txt2img
76
- result_image = image_gen.txt2img(prompt, gen_config)
77
-
78
- # Free memory by deleting model components (following main_duplicate.py pattern)
79
- if image_gen.model:
80
- if model == "sdxl":
81
- if hasattr(image_gen.model, "text_encoder_1"):
82
- del image_gen.model.text_encoder_1
83
- if hasattr(image_gen.model, "text_encoder_2"):
84
- del image_gen.model.text_encoder_2
85
- else:
86
- if hasattr(image_gen.model, "text_encoder"):
87
- del image_gen.model.text_encoder
88
-
89
- if hasattr(image_gen.model, "unet"):
90
- del image_gen.model.unet
91
- if hasattr(image_gen.model, "sampler"):
92
- del image_gen.model.sampler
93
-
94
- # Get peak memory usage
95
- peak_mem_unet = mx.metal.get_peak_memory() / 1024**3
96
-
97
- # Convert to PIL and save
98
- image_np = result_image.to_numpy()
99
- image_pil = PILImage.fromarray((image_np * 255).astype(np.uint8))
100
- image_pil.save(output)
101
-
102
- print(f"Text-to-image output saved to: {output}")
103
-
104
- # Get final peak memory usage
105
- peak_mem_overall = mx.metal.get_peak_memory() / 1024**3
106
-
107
- # Report memory usage
108
- if verbose:
109
- print(f"Peak memory used for unet: {peak_mem_unet:.3f}GB")
110
- print(f"Peak memory used overall: {peak_mem_overall:.3f}GB")
111
-
112
- # Clean up
113
- image_gen.close()
114
-
115
- return output
116
-
117
-
118
- def test_image2image(
119
- prompt="A lit fireplace",
120
- model="sdxl",
121
- strength=0.5,
122
- local_model_path="",
123
- n_images=1,
124
- steps=None,
125
- cfg=None,
126
- negative_prompt="",
127
- n_rows=1,
128
- decoding_batch_size=1,
129
- quantize=False,
130
- float16=True,
131
- preload_models=False,
132
- init_image_path="out_txt2img.png",
133
- output="out_img2img.png",
134
- verbose=False,
135
- seed=None,
136
- width=256,
137
- height=256,
138
- ):
139
- """Generate images from image and text prompt using high-level interface"""
140
-
141
- # Determine model path based on model type
142
- if model == "sdxl":
143
- model_path = local_model_path or "stabilityai/sdxl-turbo"
144
- default_cfg = 0.0
145
- default_steps = 2
146
- else:
147
- model_path = local_model_path or "stabilityai/stable-diffusion-2-1-base"
148
- default_cfg = 7.5
149
- default_steps = 50
150
-
151
- # Use provided values or defaults
152
- cfg = cfg or default_cfg
153
- steps = steps or default_steps
154
-
155
- # Load and process input image
156
- try:
157
- pil_img = PILImage.open(init_image_path)
158
- # Ensure RGB format
159
- if pil_img.mode != "RGB":
160
- pil_img = pil_img.convert("RGB")
161
-
162
- # Convert to numpy array and then to our Image class
163
- img_np = np.array(pil_img).astype(np.float32) / 255.0 # Normalize to [0,1]
164
- init_image = Image.from_numpy(img_np)
165
-
166
- except FileNotFoundError:
167
- print(f"Error: Image file '{init_image_path}' not found.")
168
- return None
169
- except Exception as e:
170
- print(f"Error loading image: {e}")
171
- return None
172
-
173
- # Create ImageGen instance
174
- image_gen = ImageGen(model_path, "", device=None)
175
-
176
- # Load the model
177
- if not image_gen.load_model(model_path):
178
- print(f"Failed to load model: {model_path}")
179
- return None
180
-
181
- # Create sampler configuration
182
- sampler_config = ImageSamplerConfig(
183
- method="ddim",
184
- steps=steps,
185
- guidance_scale=cfg,
186
- seed=seed if seed is not None else -1,
187
- )
188
-
189
- # Create generation configuration
190
- gen_config = ImageGenerationConfig(
191
- prompts=prompt,
192
- negative_prompts=negative_prompt,
193
- height=height,
194
- width=width,
195
- sampler_config=sampler_config,
196
- init_image=init_image,
197
- strength=strength,
198
- )
199
-
200
- if verbose:
201
- print(f"Generating image with prompt: '{prompt}' and strength: {strength}")
202
- print(f"Model: {model_path}, Steps: {steps}, CFG: {cfg}")
203
-
204
- # Generate image using img2img
205
- result_image = image_gen.img2img(init_image, prompt, gen_config)
206
-
207
- # Free memory by deleting model components (following main_duplicate.py pattern)
208
- if image_gen.model:
209
- if model == "sdxl":
210
- if hasattr(image_gen.model, "text_encoder_1"):
211
- del image_gen.model.text_encoder_1
212
- if hasattr(image_gen.model, "text_encoder_2"):
213
- del image_gen.model.text_encoder_2
214
- else:
215
- if hasattr(image_gen.model, "text_encoder"):
216
- del image_gen.model.text_encoder
217
-
218
- if hasattr(image_gen.model, "unet"):
219
- del image_gen.model.unet
220
- if hasattr(image_gen.model, "sampler"):
221
- del image_gen.model.sampler
222
-
223
- # Get peak memory usage
224
- peak_mem_unet = mx.metal.get_peak_memory() / 1024**3
225
-
226
- # Convert to PIL and save
227
- image_np = result_image.to_numpy()
228
- image_pil = PILImage.fromarray((image_np * 255).astype(np.uint8))
229
- image_pil.save(output)
230
-
231
- print(f"Image-to-image output saved to: {output}")
232
-
233
- # Get final peak memory usage
234
- peak_mem_overall = mx.metal.get_peak_memory() / 1024**3
235
-
236
- # Report memory usage
237
- if verbose:
238
- print(f"Peak memory used for unet: {peak_mem_unet:.3f}GB")
239
- print(f"Peak memory used overall: {peak_mem_overall:.3f}GB")
240
-
241
- # Clean up
242
- image_gen.close()
243
-
244
- return output
245
-
246
-
247
- if __name__ == "__main__":
248
- # Text-to-image parameters
249
- txt2img_params = {
250
- "prompt": "A photo of an astronaut riding a horse on Mars.",
251
- "model": "sdxl",
252
- "n_images": 1,
253
- "n_rows": 1,
254
- "output": "out_txt2img.png",
255
- "verbose": True,
256
- "width": 256,
257
- "height": 256,
258
- }
259
-
260
- # Image-to-image parameters
261
- img2img_params = {
262
- "prompt": "A lit fireplace",
263
- "model": "sdxl",
264
- "strength": 0.5,
265
- "n_images": 1,
266
- "n_rows": 1,
267
- "init_image_path": "out_txt2img.png",
268
- "output": "out_img2img.png",
269
- "verbose": True,
270
- "width": 512,
271
- "height": 512,
272
- }
273
-
274
- print("Running text-to-image generation...")
275
- generated_image = test_txt2image(**txt2img_params)
276
-
277
- if generated_image:
278
- print(f"\nRunning image-to-image generation using: {generated_image}")
279
- img2img_params["init_image_path"] = generated_image
280
- test_image2image(**img2img_params)
281
-
282
- print(f"\nPipeline complete!")
283
- print(f"Text-to-image result: {txt2img_params['output']}")
284
- print(f"Image-to-image result: {img2img_params['output']}")
285
- else:
286
- print("Failed to generate initial image, skipping img2img test")
@@ -1,306 +0,0 @@
1
- # Copyright © 2023-2024 Apple Inc.
2
-
3
- import time
4
- from typing import Optional, Tuple
5
-
6
- import mlx.core as mx
7
-
8
- from .model_io import (
9
- _DEFAULT_MODEL,
10
- load_autoencoder,
11
- load_diffusion_config,
12
- load_text_encoder,
13
- load_tokenizer,
14
- load_unet,
15
- )
16
- from .sampler import SimpleEulerAncestralSampler, SimpleEulerSampler
17
-
18
-
19
- class StableDiffusion:
20
- def __init__(self, model: str = _DEFAULT_MODEL, float16: bool = False):
21
- self.dtype = mx.float16 if float16 else mx.float32
22
- self.diffusion_config = load_diffusion_config(model)
23
- self.unet = load_unet(model, float16)
24
- self.text_encoder = load_text_encoder(model, float16)
25
- self.autoencoder = load_autoencoder(model, False)
26
- self.sampler = SimpleEulerSampler(self.diffusion_config)
27
- self.tokenizer = load_tokenizer(model)
28
-
29
- def ensure_models_are_loaded(self):
30
- mx.eval(self.unet.parameters())
31
- mx.eval(self.text_encoder.parameters())
32
- mx.eval(self.autoencoder.parameters())
33
-
34
- def _tokenize(self, tokenizer, text: str, negative_text: Optional[str] = None):
35
- # Tokenize the text
36
- tokens = [tokenizer.tokenize(text)]
37
- if negative_text is not None:
38
- tokens += [tokenizer.tokenize(negative_text)]
39
- lengths = [len(t) for t in tokens]
40
- N = max(lengths)
41
- tokens = [t + [0] * (N - len(t)) for t in tokens]
42
- tokens = mx.array(tokens)
43
-
44
- return tokens
45
-
46
- def _get_text_conditioning(
47
- self,
48
- text: str,
49
- n_images: int = 1,
50
- cfg_weight: float = 7.5,
51
- negative_text: str = "",
52
- ):
53
- # Tokenize the text
54
- tokens = self._tokenize(
55
- self.tokenizer, text, (negative_text if cfg_weight > 1 else None)
56
- )
57
-
58
- # Compute the features
59
- conditioning = self.text_encoder(tokens).last_hidden_state
60
-
61
- # Repeat the conditioning for each of the generated images
62
- if n_images > 1:
63
- conditioning = mx.repeat(conditioning, n_images, axis=0)
64
-
65
- return conditioning
66
-
67
- def _denoising_step(
68
- self, x_t, t, t_prev, conditioning, cfg_weight: float = 7.5, text_time=None
69
- ):
70
- x_t_unet = mx.concatenate([x_t] * 2, axis=0) if cfg_weight > 1 else x_t
71
- t_unet = mx.broadcast_to(t, [len(x_t_unet)])
72
- eps_pred = self.unet(
73
- x_t_unet, t_unet, encoder_x=conditioning, text_time=text_time
74
- )
75
-
76
- if cfg_weight > 1:
77
- eps_text, eps_neg = eps_pred.split(2)
78
- eps_pred = eps_neg + cfg_weight * (eps_text - eps_neg)
79
-
80
- x_t_prev = self.sampler.step(eps_pred, x_t, t, t_prev)
81
-
82
- return x_t_prev
83
-
84
- def _denoising_loop(
85
- self,
86
- x_T,
87
- T,
88
- conditioning,
89
- num_steps: int = 50,
90
- cfg_weight: float = 7.5,
91
- text_time=None,
92
- ):
93
- x_t = x_T
94
- for t, t_prev in self.sampler.timesteps(
95
- num_steps, start_time=T, dtype=self.dtype
96
- ):
97
- x_t = self._denoising_step(
98
- x_t, t, t_prev, conditioning, cfg_weight, text_time
99
- )
100
- yield x_t
101
-
102
- def generate_latents(
103
- self,
104
- text: str,
105
- n_images: int = 1,
106
- num_steps: int = 50,
107
- cfg_weight: float = 7.5,
108
- negative_text: str = "",
109
- latent_size: Tuple[int] = (64, 64),
110
- seed=None,
111
- ):
112
- # Set the PRNG state
113
- seed = int(time.time()) if seed is None else seed
114
- mx.random.seed(seed)
115
-
116
- # Get the text conditioning
117
- conditioning = self._get_text_conditioning(
118
- text, n_images, cfg_weight, negative_text
119
- )
120
-
121
- # Create the latent variables
122
- x_T = self.sampler.sample_prior(
123
- (n_images, *latent_size, self.autoencoder.latent_channels), dtype=self.dtype
124
- )
125
-
126
- # Perform the denoising loop
127
- yield from self._denoising_loop(
128
- x_T, self.sampler.max_time, conditioning, num_steps, cfg_weight
129
- )
130
-
131
- def generate_latents_from_image(
132
- self,
133
- image,
134
- text: str,
135
- n_images: int = 1,
136
- strength: float = 0.8,
137
- num_steps: int = 50,
138
- cfg_weight: float = 7.5,
139
- negative_text: str = "",
140
- seed=None,
141
- ):
142
- # Set the PRNG state
143
- seed = int(time.time()) if seed is None else seed
144
- mx.random.seed(seed)
145
-
146
- # Define the num steps and start step
147
- start_step = self.sampler.max_time * strength
148
- num_steps = int(num_steps * strength)
149
-
150
- # Get the text conditioning
151
- conditioning = self._get_text_conditioning(
152
- text, n_images, cfg_weight, negative_text
153
- )
154
-
155
- # Get the latents from the input image and add noise according to the
156
- # start time.
157
- x_0, _ = self.autoencoder.encode(image[None])
158
- x_0 = mx.broadcast_to(x_0, (n_images,) + x_0.shape[1:])
159
- x_T = self.sampler.add_noise(x_0, mx.array(start_step))
160
-
161
- # Perform the denoising loop
162
- yield from self._denoising_loop(
163
- x_T, start_step, conditioning, num_steps, cfg_weight
164
- )
165
-
166
- def decode(self, x_t):
167
- x = self.autoencoder.decode(x_t)
168
- x = mx.clip(x / 2 + 0.5, 0, 1)
169
- return x
170
-
171
-
172
- class StableDiffusionXL(StableDiffusion):
173
- def __init__(self, model: str = _DEFAULT_MODEL, float16: bool = False):
174
- super().__init__(model, float16)
175
-
176
- self.sampler = SimpleEulerAncestralSampler(self.diffusion_config)
177
-
178
- self.text_encoder_1 = self.text_encoder
179
- self.tokenizer_1 = self.tokenizer
180
- del self.tokenizer, self.text_encoder
181
-
182
- self.text_encoder_2 = load_text_encoder(
183
- model,
184
- float16,
185
- model_key="text_encoder_2",
186
- )
187
- self.tokenizer_2 = load_tokenizer(
188
- model,
189
- merges_key="tokenizer_2_merges",
190
- vocab_key="tokenizer_2_vocab",
191
- )
192
-
193
- def ensure_models_are_loaded(self):
194
- mx.eval(self.unet.parameters())
195
- mx.eval(self.text_encoder_1.parameters())
196
- mx.eval(self.text_encoder_2.parameters())
197
- mx.eval(self.autoencoder.parameters())
198
-
199
- def _get_text_conditioning(
200
- self,
201
- text: str,
202
- n_images: int = 1,
203
- cfg_weight: float = 7.5,
204
- negative_text: str = "",
205
- ):
206
- tokens_1 = self._tokenize(
207
- self.tokenizer_1,
208
- text,
209
- (negative_text if cfg_weight > 1 else None),
210
- )
211
- tokens_2 = self._tokenize(
212
- self.tokenizer_2,
213
- text,
214
- (negative_text if cfg_weight > 1 else None),
215
- )
216
-
217
- conditioning_1 = self.text_encoder_1(tokens_1)
218
- conditioning_2 = self.text_encoder_2(tokens_2)
219
- conditioning = mx.concatenate(
220
- [conditioning_1.hidden_states[-2], conditioning_2.hidden_states[-2]],
221
- axis=-1,
222
- )
223
- pooled_conditioning = conditioning_2.pooled_output
224
-
225
- if n_images > 1:
226
- conditioning = mx.repeat(conditioning, n_images, axis=0)
227
- pooled_conditioning = mx.repeat(pooled_conditioning, n_images, axis=0)
228
-
229
- return conditioning, pooled_conditioning
230
-
231
- def generate_latents(
232
- self,
233
- text: str,
234
- n_images: int = 1,
235
- num_steps: int = 2,
236
- cfg_weight: float = 0.0,
237
- negative_text: str = "",
238
- latent_size: Tuple[int] = (64, 64),
239
- seed=None,
240
- ):
241
- # Set the PRNG state
242
- seed = int(time.time()) if seed is None else seed
243
- mx.random.seed(seed)
244
-
245
- # Get the text conditioning
246
- conditioning, pooled_conditioning = self._get_text_conditioning(
247
- text, n_images, cfg_weight, negative_text
248
- )
249
- text_time = (
250
- pooled_conditioning,
251
- mx.array([[512, 512, 0, 0, 512, 512.0]] * len(pooled_conditioning)),
252
- )
253
-
254
- # Create the latent variables
255
- x_T = self.sampler.sample_prior(
256
- (n_images, *latent_size, self.autoencoder.latent_channels), dtype=self.dtype
257
- )
258
-
259
- # Perform the denoising loop
260
- yield from self._denoising_loop(
261
- x_T,
262
- self.sampler.max_time,
263
- conditioning,
264
- num_steps,
265
- cfg_weight,
266
- text_time=text_time,
267
- )
268
-
269
- def generate_latents_from_image(
270
- self,
271
- image,
272
- text: str,
273
- n_images: int = 1,
274
- strength: float = 0.8,
275
- num_steps: int = 2,
276
- cfg_weight: float = 0.0,
277
- negative_text: str = "",
278
- seed=None,
279
- ):
280
- # Set the PRNG state
281
- seed = seed or int(time.time())
282
- mx.random.seed(seed)
283
-
284
- # Define the num steps and start step
285
- start_step = self.sampler.max_time * strength
286
- num_steps = int(num_steps * strength)
287
-
288
- # Get the text conditioning
289
- conditioning, pooled_conditioning = self._get_text_conditioning(
290
- text, n_images, cfg_weight, negative_text
291
- )
292
- text_time = (
293
- pooled_conditioning,
294
- mx.array([[512, 512, 0, 0, 512, 512.0]] * len(pooled_conditioning)),
295
- )
296
-
297
- # Get the latents from the input image and add noise according to the
298
- # start time.
299
- x_0, _ = self.autoencoder.encode(image[None])
300
- x_0 = mx.broadcast_to(x_0, (n_images,) + x_0.shape[1:])
301
- x_T = self.sampler.add_noise(x_0, mx.array(start_step))
302
-
303
- # Perform the denoising loop
304
- yield from self._denoising_loop(
305
- x_T, start_step, conditioning, num_steps, cfg_weight, text_time=text_time
306
- )
@@ -1,116 +0,0 @@
1
- # Copyright © 2023-2024 Apple Inc.
2
-
3
- from dataclasses import dataclass
4
- from typing import List, Optional
5
-
6
- import mlx.core as mx
7
- import mlx.nn as nn
8
-
9
- from .config import CLIPTextModelConfig
10
-
11
- _ACTIVATIONS = {"quick_gelu": nn.gelu_fast_approx, "gelu": nn.gelu}
12
-
13
-
14
- @dataclass
15
- class CLIPOutput:
16
- # The last_hidden_state indexed at the EOS token and possibly projected if
17
- # the model has a projection layer
18
- pooled_output: Optional[mx.array] = None
19
-
20
- # The full sequence output of the transformer after the final layernorm
21
- last_hidden_state: Optional[mx.array] = None
22
-
23
- # A list of hidden states corresponding to the outputs of the transformer layers
24
- hidden_states: Optional[List[mx.array]] = None
25
-
26
-
27
- class CLIPEncoderLayer(nn.Module):
28
- """The transformer encoder layer from CLIP."""
29
-
30
- def __init__(self, model_dims: int, num_heads: int, activation: str):
31
- super().__init__()
32
-
33
- self.layer_norm1 = nn.LayerNorm(model_dims)
34
- self.layer_norm2 = nn.LayerNorm(model_dims)
35
-
36
- self.attention = nn.MultiHeadAttention(model_dims, num_heads)
37
- # Add biases to the attention projections to match CLIP
38
- self.attention.query_proj.bias = mx.zeros(model_dims)
39
- self.attention.key_proj.bias = mx.zeros(model_dims)
40
- self.attention.value_proj.bias = mx.zeros(model_dims)
41
- self.attention.out_proj.bias = mx.zeros(model_dims)
42
-
43
- self.linear1 = nn.Linear(model_dims, 4 * model_dims)
44
- self.linear2 = nn.Linear(4 * model_dims, model_dims)
45
-
46
- self.act = _ACTIVATIONS[activation]
47
-
48
- def __call__(self, x, attn_mask=None):
49
- y = self.layer_norm1(x)
50
- y = self.attention(y, y, y, attn_mask)
51
- x = y + x
52
-
53
- y = self.layer_norm2(x)
54
- y = self.linear1(y)
55
- y = self.act(y)
56
- y = self.linear2(y)
57
- x = y + x
58
-
59
- return x
60
-
61
-
62
- class CLIPTextModel(nn.Module):
63
- """Implements the text encoder transformer from CLIP."""
64
-
65
- def __init__(self, config: CLIPTextModelConfig):
66
- super().__init__()
67
-
68
- self.token_embedding = nn.Embedding(config.vocab_size, config.model_dims)
69
- self.position_embedding = nn.Embedding(config.max_length, config.model_dims)
70
- self.layers = [
71
- CLIPEncoderLayer(config.model_dims, config.num_heads, config.hidden_act)
72
- for i in range(config.num_layers)
73
- ]
74
- self.final_layer_norm = nn.LayerNorm(config.model_dims)
75
-
76
- if config.projection_dim is not None:
77
- self.text_projection = nn.Linear(
78
- config.model_dims, config.projection_dim, bias=False
79
- )
80
-
81
- def _get_mask(self, N, dtype):
82
- indices = mx.arange(N)
83
- mask = indices[:, None] < indices[None]
84
- mask = mask.astype(dtype) * (-6e4 if dtype == mx.float16 else -1e9)
85
- return mask
86
-
87
- def __call__(self, x):
88
- # Extract some shapes
89
- B, N = x.shape
90
- eos_tokens = x.argmax(-1)
91
-
92
- # Compute the embeddings
93
- x = self.token_embedding(x)
94
- x = x + self.position_embedding.weight[:N]
95
-
96
- # Compute the features from the transformer
97
- mask = self._get_mask(N, x.dtype)
98
- hidden_states = []
99
- for l in self.layers:
100
- x = l(x, mask)
101
- hidden_states.append(x)
102
-
103
- # Apply the final layernorm and return
104
- x = self.final_layer_norm(x)
105
- last_hidden_state = x
106
-
107
- # Select the EOS token
108
- pooled_output = x[mx.arange(len(x)), eos_tokens]
109
- if "text_projection" in self:
110
- pooled_output = self.text_projection(pooled_output)
111
-
112
- return CLIPOutput(
113
- pooled_output=pooled_output,
114
- last_hidden_state=last_hidden_state,
115
- hidden_states=hidden_states,
116
- )