Model.to() doesnt working. Error: AttributeError: 'NoneType' object has no attribute 'to'

oblivisheee: <p>Hello, i trying to make txt2img stable diffusion generation, but, faced with a problem when i trying to use model.to() and model.eval() it raise error. Here is a code:</p>
<pre><code class="lang-auto">import torch
from samplers import sampler_list as sm
import torch.nn as nn

class generate_txt2img(nn.Module):
    def __init__(self, model, prompt, guidance_scale, width, height, steps, sampler='dpmsolver++', seed=None, negative_prompt=None):
        super(generate_txt2img, self).__init__()
        device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
        self.model = model.to(device)  
        model.eval()
        if negative_prompt is None:
            negative_prompt_hid = ""
            negative_prompt = negative_prompt_hid
        image = torch.randn(1, 3, height, width, device=device)
        if seed is not None:
            torch.manual_seed(seed)
        if sampler == 'dpmsolver++':
            sampler_var = sm.dpm_solver_plus(model=self.model) 
        elif sampler == 'dpmsolver':
            sampler_var = sm.dpm_solver(model=self.model)
        with torch.no_grad():
            for _ in range(steps):
                z = torch.randn_like(image)
                positive_loss = self.model(image, z, prompt, guidance_scale)
                if negative_prompt:
                    negative_loss = self.model(image, z, negative_prompt, None, guidance_scale)
                    loss = positive_loss - negative_loss
                else:
                    loss = positive_loss
                self.model.zero_grad()
                loss.backward()
                image = sampler_var(sampler, image, image.grad)
                image.clamp_(0, 1)
        self.generated_image = image[0].cpu()

</code></pre>
<p>P.S: Model loading from other script which i made thru variable.</p>
<p>Will be glad if you will help me)</p>

yiftach: <p>The title means this is a python error not specific to PyTorch - the object you call <code>.to()</code> on is <code>None</code>. You’ll have to show use the code that calls <code>generate_txt2img()</code> for us to help.<br>
Also, <code>generate_txt2img()</code> doesn’t need to be an <code>nn.Module</code> - you can use a function:</p>
<pre><code class="lang-auto">def generate_txt2img(model, prompt, guidance_scale, width, height, steps, sampler='dpmsolver++', seed=None, negative_prompt=None):
    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
    self.model = model.to(device)  
    # ... etc
</code></pre>

oblivisheee: <aside class="quote no-group" data-username="yiftach" data-post="2" data-topic="188486">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/yiftach/48/58678_2.png" class="avatar"> yiftach:</div>
<blockquote>
<p>The title means this is a python error not specific to PyTorch - the object you call <code>.to()</code> on is <code>None</code>. You’ll have to show use the code that calls <code>generate_txt2img()</code> for us to help.<br>
Also, <code>generate_txt2img()</code> doesn’t need to be an <code>nn.Module</code> - you can use a function:</p>
</blockquote>
</aside>
<p>here is a full code which call all components:</p>
<pre><code class="lang-auto">from download_components import download_sd_base as sd_download
from load_models import load_model as lm
import os
from other_components import model_manager
import time
import torch
from generation_components.txt2img import generate_txt2img
save_path = "base_models"
if not os.path.exists(save_path):
    os.makedirs(save_path)
#files_with_extensions = [file for file in os.listdir(save_path) if file.endswith((".ckpt", ".safetensors"))]
if not os.path.isfile(save_path + "/sd-v1-5-pruned.safetensors"):
    sd_download.download_model_sd(save_path=save_path, model='sd', model_ext="safetensors")
loaded_model = lm.load_model(model_path=save_path + '/sd-v1-5-pruned.safetensors')
time.sleep(1)
os.system('clear')

generate_txt2img(model=loaded_model,
                    prompt="A man, sitting on chair", negative_prompt="NSFW",
                    guidance_scale=10, width=512, height=512, steps=25, seed=2582485, sampler='dpmsolver++')
</code></pre>

yiftach: <p>It seems <code>model</code> from the previous snippet is <code>loaded_model</code>, which is instantiated using <code>lm.load_model()</code>, which is not shown here. You’ll have to understand why that function returns <code>None</code> - it could be due to a missing file at the supplied path.</p>

oblivisheee: <p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/5/3/5382dbfd68db52605e7749e8dc237c815b98473a.png" data-download-href="https://discuss.pytorch.org/uploads/default/5382dbfd68db52605e7749e8dc237c815b98473a" title="Screenshot 2023-09-17 at 16.11.24"><img src="https://discuss.pytorch.org/uploads/default/original/3X/5/3/5382dbfd68db52605e7749e8dc237c815b98473a.png" alt="Screenshot 2023-09-17 at 16.11.24" data-base62-sha1="bULZGjStcy55XWFscRkhKXNv6ie" width="690" height="141" data-dominant-color="3D3D3D"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">Screenshot 2023-09-17 at 16.11.24</span><span class="informations">2104×430 57.3 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div><br>
it sending weights, while i starting script, and, looks like it really load model:</p>
<pre><code class="lang-auto">import torch
from safetensors.torch import load_file

def load_model(model_path: str) -&gt; torch.nn.Module:
    if ".ckpt" in model_path:
        try:
            checkpoint = torch.load(model_path)
            model = torch.nn.Module()
            model.load_state_dict(checkpoint['state_dict'])
            print("Model has been successfully loaded.")
            return model
        except FileNotFoundError:
            print(f"Error while loading model: File '{model_path}' not found.")
        except Exception as e:
            print(f"Error while loading model: {str(e)}")
    elif ".safetensors" in model_path:
        try:
            model = torch.nn.Module()
            checkpoint = load_file(model_path)
            model.load_state_dict(checkpoint)
            print("Model has been successfully loaded.")
            return model
        except FileNotFoundError:
            print(f"Error while loading model: File '{model_path}' not found.")
        except Exception as e:
            print(f"Error while loading model: {str(e)}")
    else:
        raise ValueError('Your model is unsupported.\nUse model with ".ckpt" or ".safetensors" extensions.')
</code></pre>

ptrblck: <p>Replace the <code>print</code> statements with <code>assert</code>s as I guess your model was never loaded and you might have ignored the printed warning.</p>