comfyui.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. package llm_container
  2. import (
  3. "context"
  4. commonapi "yunion.io/x/onecloud/pkg/apis"
  5. computeapi "yunion.io/x/onecloud/pkg/apis/compute"
  6. api "yunion.io/x/onecloud/pkg/apis/llm"
  7. "yunion.io/x/onecloud/pkg/llm/models"
  8. "yunion.io/x/onecloud/pkg/mcclient"
  9. )
  10. func init() {
  11. models.RegisterLLMContainerDriver(newComfyUI())
  12. }
  13. type comfyui struct {
  14. baseDriver
  15. }
  16. func newComfyUI() models.ILLMContainerDriver {
  17. return &comfyui{baseDriver: newBaseDriver(api.LLM_CONTAINER_COMFYUI)}
  18. }
  19. func (c *comfyui) GetSpec(sku *models.SLLMSku) interface{} {
  20. if sku.LLMSpec == nil {
  21. return nil
  22. }
  23. return sku.LLMSpec.ComfyUI
  24. }
  25. func (c *comfyui) GetEffectiveSpec(llm *models.SLLM, sku *models.SLLMSku) interface{} {
  26. if llm != nil && llm.LLMSpec != nil && llm.LLMSpec.ComfyUI != nil {
  27. return llm.LLMSpec.ComfyUI
  28. }
  29. return c.GetSpec(sku)
  30. }
  31. func (c *comfyui) GetContainerSpec(ctx context.Context, llm *models.SLLM, image *models.SLLMImage, sku *models.SLLMSku, props []string, devices []computeapi.SIsolatedDevice, diskId string) *computeapi.PodContainerCreateInput {
  32. spec := computeapi.ContainerSpec{
  33. ContainerSpec: commonapi.ContainerSpec{
  34. Image: image.ToContainerImage(),
  35. ImageCredentialId: image.CredentialId,
  36. EnableLxcfs: true,
  37. AlwaysRestart: true,
  38. Envs: []*commonapi.ContainerKeyValue{
  39. {
  40. Key: "CLI_ARGS",
  41. Value: "--disable-xformers",
  42. },
  43. },
  44. },
  45. }
  46. if len(devices) == 0 && (sku.Devices != nil && len(*sku.Devices) > 0) {
  47. for i := range *sku.Devices {
  48. index := i
  49. spec.Devices = append(spec.Devices, &computeapi.ContainerDevice{
  50. Type: commonapi.CONTAINER_DEVICE_TYPE_ISOLATED_DEVICE,
  51. IsolatedDevice: &computeapi.ContainerIsolatedDevice{
  52. Index: &index,
  53. },
  54. })
  55. }
  56. } else if len(devices) > 0 {
  57. for i := range devices {
  58. spec.Devices = append(spec.Devices, &computeapi.ContainerDevice{
  59. Type: commonapi.CONTAINER_DEVICE_TYPE_ISOLATED_DEVICE,
  60. IsolatedDevice: &computeapi.ContainerIsolatedDevice{
  61. Id: devices[i].Id,
  62. },
  63. })
  64. }
  65. }
  66. // Volume Mounts, see: https://github.com/YanWenKun/ComfyUI-Docker?tab=readme-ov-file#quick-start---nvidia-gpu
  67. diskIndex := 0
  68. ctrVols := []*commonapi.ContainerVolumeMount{
  69. {
  70. Disk: &commonapi.ContainerVolumeMountDisk{
  71. Index: &diskIndex,
  72. SubDirectory: "storage",
  73. },
  74. Type: commonapi.CONTAINER_VOLUME_MOUNT_TYPE_DISK,
  75. MountPath: "/root",
  76. },
  77. {
  78. Disk: &commonapi.ContainerVolumeMountDisk{
  79. Index: &diskIndex,
  80. SubDirectory: "storage-models/models",
  81. },
  82. Type: commonapi.CONTAINER_VOLUME_MOUNT_TYPE_DISK,
  83. MountPath: "/root/ComfyUI/models",
  84. },
  85. {
  86. Disk: &commonapi.ContainerVolumeMountDisk{
  87. Index: &diskIndex,
  88. SubDirectory: "storage-models/hf-hub",
  89. },
  90. Type: commonapi.CONTAINER_VOLUME_MOUNT_TYPE_DISK,
  91. MountPath: "/root/.cache/huggingface/hub",
  92. },
  93. {
  94. Disk: &commonapi.ContainerVolumeMountDisk{
  95. Index: &diskIndex,
  96. SubDirectory: "storage-models/torch-hub",
  97. },
  98. Type: commonapi.CONTAINER_VOLUME_MOUNT_TYPE_DISK,
  99. MountPath: "/root/.cache/torch/hub",
  100. },
  101. {
  102. Disk: &commonapi.ContainerVolumeMountDisk{
  103. Index: &diskIndex,
  104. SubDirectory: "storage-user/input",
  105. },
  106. Type: commonapi.CONTAINER_VOLUME_MOUNT_TYPE_DISK,
  107. MountPath: "/root/ComfyUI/input",
  108. },
  109. {
  110. Disk: &commonapi.ContainerVolumeMountDisk{
  111. Index: &diskIndex,
  112. SubDirectory: "storage-user/output",
  113. },
  114. Type: commonapi.CONTAINER_VOLUME_MOUNT_TYPE_DISK,
  115. MountPath: "/root/ComfyUI/output",
  116. },
  117. {
  118. Disk: &commonapi.ContainerVolumeMountDisk{
  119. Index: &diskIndex,
  120. SubDirectory: "storage-user/workflows",
  121. },
  122. Type: commonapi.CONTAINER_VOLUME_MOUNT_TYPE_DISK,
  123. MountPath: "/root/ComfyUI/user/default/workflows",
  124. },
  125. }
  126. spec.VolumeMounts = append(spec.VolumeMounts, ctrVols...)
  127. return &computeapi.PodContainerCreateInput{
  128. ContainerSpec: spec,
  129. }
  130. }
  131. func (c *comfyui) GetContainerSpecs(ctx context.Context, llm *models.SLLM, image *models.SLLMImage, sku *models.SLLMSku, props []string, devices []computeapi.SIsolatedDevice, diskId string) []*computeapi.PodContainerCreateInput {
  132. return []*computeapi.PodContainerCreateInput{
  133. c.GetContainerSpec(ctx, llm, image, sku, props, devices, diskId),
  134. }
  135. }
  136. func (c *comfyui) GetLLMAccessUrlInfo(ctx context.Context, userCred mcclient.TokenCredential, llm *models.SLLM, input *models.LLMAccessInfoInput) (*api.LLMAccessUrlInfo, error) {
  137. return models.GetLLMAccessUrlInfo(ctx, userCred, llm, input, "http", 8188)
  138. }
  139. func (c *comfyui) GetProbedInstantModelsExt(ctx context.Context, userCred mcclient.TokenCredential, llm *models.SLLM, mdlIds ...string) (map[string]api.LLMInternalInstantMdlInfo, error) {
  140. return nil, nil
  141. }
  142. func (c *comfyui) DetectModelPaths(ctx context.Context, userCred mcclient.TokenCredential, llm *models.SLLM, pkgInfo api.LLMInternalInstantMdlInfo) ([]string, error) {
  143. return nil, nil
  144. }
  145. func (c *comfyui) GetImageInternalPathMounts(sApp *models.SInstantModel) map[string]string {
  146. return nil
  147. }
  148. func (c *comfyui) GetSaveDirectories(sApp *models.SInstantModel) (string, []string, error) {
  149. return "", nil, nil
  150. }
  151. func (c *comfyui) ValidateMounts(mounts []string, mdlName string, mdlTag string) ([]string, error) {
  152. return nil, nil
  153. }
  154. func (c *comfyui) CheckDuplicateMounts(errStr string, dupIndex int) string {
  155. return "Duplicate mounts detected"
  156. }
  157. func (c *comfyui) GetInstantModelIdByPostOverlay(postOverlay *commonapi.ContainerVolumeMountDiskPostOverlay, mdlNameToId map[string]string) string {
  158. return ""
  159. }
  160. func (c *comfyui) GetDirPostOverlay(dir api.LLMMountDirInfo) *commonapi.ContainerVolumeMountDiskPostOverlay {
  161. return nil
  162. }
  163. func (c *comfyui) PreInstallModel(ctx context.Context, userCred mcclient.TokenCredential, llm *models.SLLM, instMdl *models.SLLMInstantModel) error {
  164. return nil
  165. }
  166. func (c *comfyui) InstallModel(ctx context.Context, userCred mcclient.TokenCredential, llm *models.SLLM, dirs []string, mdlIds []string) error {
  167. return nil
  168. }
  169. func (c *comfyui) UninstallModel(ctx context.Context, userCred mcclient.TokenCredential, llm *models.SLLM, instMdl *models.SLLMInstantModel) error {
  170. return nil
  171. }
  172. func (c *comfyui) DownloadModel(ctx context.Context, userCred mcclient.TokenCredential, llm *models.SLLM, tmpDir string, modelName string, modelTag string) (string, []string, error) {
  173. return "", nil, nil
  174. }