HelloSun commited on
Commit
ba3f99d
·
verified ·
1 Parent(s): 245705c

Update inference.py

Browse files
Files changed (1) hide show
  1. inference.py +17 -48
inference.py CHANGED
@@ -1,61 +1,30 @@
1
- import os
2
-
3
- # ===== CPU optimization =====
4
- CPU_THREADS = str(os.cpu_count() or 2)
5
-
6
- os.environ["OMP_NUM_THREADS"] = CPU_THREADS
7
- os.environ["OPENVINO_NUM_THREADS"] = CPU_THREADS
8
- os.environ["MKL_NUM_THREADS"] = CPU_THREADS
9
- os.environ["NUMEXPR_NUM_THREADS"] = CPU_THREADS
10
- os.environ["OMP_WAIT_POLICY"] = "PASSIVE"
11
- os.environ["KMP_BLOCKTIME"] = "0"
12
-
13
  import torch
14
  from optimum.intel import OVZImagePipeline
15
 
16
- # ===== Global =====
17
- pipe = None
18
- generator = torch.Generator("cpu")
19
-
20
-
21
- def load_model():
22
- global pipe
23
-
24
- if pipe is None:
25
- pipe = OVZImagePipeline.from_pretrained(
26
- "hsuwill000/Z-Image-Turbo-ov",
27
- device="cpu"
28
- )
29
-
30
- # warmup
31
- pipe(
32
- prompt="warmup",
33
- height=512,
34
- width=512,
35
- num_inference_steps=1,
36
- guidance_scale=0.0,
37
- )
38
-
39
- return pipe
40
 
41
 
42
  def generate(
43
- prompt: str,
44
- height: int = 512,
45
- width: int = 512,
46
- steps: int = 9,
47
- seed: int = -1,
48
  ):
49
- pipe = load_model()
50
-
51
- if seed != -1:
52
- generator.manual_seed(int(seed))
53
 
54
  image = pipe(
55
  prompt=prompt,
56
- height=height,
57
- width=width,
58
- num_inference_steps=steps,
59
  guidance_scale=0.0,
60
  generator=generator,
61
  ).images[0]
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import torch
2
  from optimum.intel import OVZImagePipeline
3
 
4
+ # ===== Load once =====
5
+ pipe = OVZImagePipeline.from_pretrained(
6
+ "hsuwill000/Z-Image-Turbo-ov",
7
+ device="cpu"
8
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
 
10
 
11
  def generate(
12
+ prompt,
13
+ height=512,
14
+ width=512,
15
+ steps=9,
16
+ seed=-1,
17
  ):
18
+ if seed == -1:
19
+ generator = torch.Generator("cpu")
20
+ else:
21
+ generator = torch.Generator("cpu").manual_seed(int(seed))
22
 
23
  image = pipe(
24
  prompt=prompt,
25
+ height=int(height),
26
+ width=int(width),
27
+ num_inference_steps=int(steps),
28
  guidance_scale=0.0,
29
  generator=generator,
30
  ).images[0]