Project: 062_Your_Mixture-of-Experts_LLM_Is_Secretly_an_Embeddi | Function: encode (iclr)

system: You are an expert Python programmer implementing algorithms from scientific papers.
Complete the masked function based on the algorithm description.
Follow Python best practices and maintain consistent code style.
Return **only** the complete Python function code.

user: Complete this Python function:

**DESCRIPTION:** Main MOEE embedding extraction and combination method that implements the paper's core approach of combining routing weights (RW) with hidden states (HS) to create superior embeddings without additional training.

**KEY TERMS:** {
  "lst_token_rw": "eRW routing weight embedding",
  "lst_token_emb": "eHS hidden state embedding",
  "embeddings": "efinal combined embedding",
  "sent_emb": "routing weights from all layers"
}

**CONTEXT CODE:**         super().__init__()
        
        self.base_model = model_name_or_path

        if 'deepseek-moe' in model_name_or_path:
            self.model, self.tokenizer = load_pretrained_model(model_name_or_path, 'deepseek-moe')
        elif 'Qwen' in model_name_or_path:
            self.model, self.tokenizer = load_pretrained_model(model_name_or_path, 'Qwen')
        elif 'OLMoE' in model_name_or_path:
            self.model, self.tokenizer = load_pretrained_model(model_name_or_path, 'OLMoE')
            
        print('self.model: ', self.model)

        if hasattr(self.model, 'model'): # LLama2 & Mistral
            self.embedding_attr = 'model'
        elif hasattr(self.model, 'transformer'): # GPT-Neo & GPT-J
            self.embedding_attr = 'transformer'
        else: 
            raise ValueError("Could not find attribute to use for embedding: ", self.model)

        self.projection = torch.nn.Linear(
            in_features=self.model.config.hidden_size, 
            out_features=int(projection),
            dtype=self.model.dtype
        ) if projection is not None else None
        self.normalized = normalized
        self.pooling_method = pooling_method
        self.device = device
        self.num_gpus = 1
        self.embed_eos = embed_eos
        self.attn = attn
        if (self.attn is not None) and self.attn not in ['bbcc', 'cccc', 'bb', 'cc']:
            raise ValueError(f"Mixed attention no longer supported: {self.attn}. Only bbcc, cccc, bb, cc are supported")

    def encode_queries(self, queries: Union[List[str], str], **kwargs) -> np.ndarray:
        """Used for encoding the queries of retrieval or reranking tasks"""
        return self.encode(queries, **kwargs)

    def encode_corpus(self, corpus: Union[List[str], str, List[Dict[str, str]]], **kwargs) -> np.ndarray:
        """Used for encoding the corpus of retrieval tasks"""
        if isinstance(corpus, dict):
            corpus = [corpus]
        if isinstance(corpus, list) and isinstance(corpus[0], dict):
            corpus = [
                doc["title"] + " " + doc["text"] if "title" in doc 
                else doc["text"] for doc in corpus
            ]
        return self.encode(corpus, **kwargs)

    @torch.no_grad()
    def encode(
        self,
        sentences: Union[List[str], str],
        batch_size: int = 256,
        max_length: int = 512,
        instruction: str = "",
        embed_instruction: bool = False,
        get_cache: bool = False,
        convert_to_tensor: bool = False,
        recast: bool = False,
        add_special_tokens: bool = True,
        **kwargs,
    ) -> np.ndarray:
        """
        Encodes sentences using the MoE model with routing weight extraction.

        This is the core MOEE method that combines hidden states with routing weights
        to create enhanced embeddings by extracting and normalizing features from both
        the standard model output and the MoE routing decisions.
        """
        raise NotImplementedError("Function implementation has been masked for testing")


Complete the function implementation replacing "raise NotImplementedError".